Type of a Triangle

easy Programming Basics

Problem

Read three side lengths. Print equilateral (all sides equal), isosceles (exactly two sides equal) or scalene (all different). If the three numbers cannot be the sides of a triangle, print not a triangle. A triangle needs positive sides, and the sum of any two sides must be bigger than the third side (equal is not enough).

Constraints

  • |side| <= 10^9

Examples

Input

3 4 5

Output

scalene

Input

1 2 3

Output

not a triangle

Solutions

Best approachTime O(1)Space O(1)

Always check the validity first: sides such as 1 2 3 would look scalene but cannot make a triangle. The comparison a + b <= c is safe from overflow here because the sides are at most 10^9 and we use 64-bit numbers.

Same code works in Java810172125Basic features only, so nothing changes between versions.
/*
 * Problem : Classify a triangle.
 * Input   : Three integers.
 * Output  : A type word.
 * Approach: Validate, then compare.
 * Time    : O(1)
 * Space   : O(1)
 * Java    : 8 or newer.
 */
import java.util.Scanner;

public class Main {
    // This method solves the task. It receives the input and returns the answer.
    static String solve(long a, long b, long m) {
        // Step 1: Check the sides first. Every side must be positive.
        if (a <= 0 || b <= 0 || m <= 0) {
            return "not a triangle";
        }
        // Step 2: The sum of any two sides must be bigger than the third side. Equal is a flat line, not a triangle.
        if (a + b <= m || a + m <= b || b + m <= a) {
            return "not a triangle";
        }
        // Step 3: Count equal sides to find the type.
        if (a == b && b == m) {
            return "equilateral";
        }
        if (a == b || b == m || a == m) {
            return "isosceles";
        }
        return "scalene";
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long a = scanner.nextLong();
        long b = scanner.nextLong();
        long m = scanner.nextLong();
        System.out.println(solve(a, b, m));
    }
}
Hints (1)
  1. Why is 1 2 3 not a triangle?