algoadvance

You are given three integers a, b, and c, which represent the lengths of the sides of a triangle. You need to determine and print the type of triangle based on the lengths of its sides.

The possible outputs are:

Constraints

Clarifying Questions

  1. Will the side lengths be positive integers?
    • Yes, the problem statement mentions they are positive integers.
  2. Do we need to handle input validation, or can we assume the input is always valid?
    • We can assume the input is valid and the given sides always form a valid triangle.

Strategy

  1. Check for Equilateral Triangle: If all three sides a, b, and c are equal, then print “Equilateral”.
  2. Check for Isosceles Triangle: If exactly two sides are equal, then print “Isosceles”.
  3. Check for Scalene Triangle: If all three sides are different, then print “Scalene”.

Since the problem guarantees the input forms a valid triangle, we don’t need to perform additional validity checks for the side lengths.

Code

def triangle_type(a: int, b: int, c: int) -> str:
    if a == b == c:
        return "Equilateral"
    elif a == b or b == c or a == c:
        return "Isosceles"
    else:
        return "Scalene"

# Example usage:
print(triangle_type(3, 3, 3))  # Should print "Equilateral"
print(triangle_type(2, 2, 3))  # Should print "Isosceles"
print(triangle_type(3, 4, 5))  # Should print "Scalene"

Time Complexity

The function performs a constant number of comparisons and operations, hence the time complexity is O(1) (constant time).

This solution is efficient and straightforward given the constraints of the problem.

Try our interview co-pilot at AlgoAdvance.com