Type of Triangle - Problem
Imagine you're a geometry teacher checking triangle homework! ๐
You are given a 3-element integer array nums representing the three sides of a potential triangle. Your task is to determine what type of triangle these sides can form, or if they can't form a triangle at all.
Triangle Types:
- Equilateral: All three sides are equal (e.g., [5, 5, 5])
- Isosceles: Exactly two sides are equal (e.g., [5, 5, 3])
- Scalene: All three sides are different (e.g., [3, 4, 5])
- None: The sides cannot form a valid triangle
Remember: For three sides to form a valid triangle, the sum of any two sides must be greater than the third side (Triangle Inequality Theorem).
Input & Output
example_1.py โ Isosceles Triangle
$
Input:
[3, 4, 5]
โบ
Output:
"scalene"
๐ก Note:
All three sides (3, 4, 5) are different lengths, and they satisfy the triangle inequality (3+4>5, 3+5>4, 4+5>3), so it forms a scalene triangle.
example_2.py โ Equilateral Triangle
$
Input:
[3, 3, 3]
โบ
Output:
"equilateral"
๐ก Note:
All three sides are equal (3 = 3 = 3), and they satisfy the triangle inequality, so it forms an equilateral triangle.
example_3.py โ Invalid Triangle
$
Input:
[1, 1, 3]
โบ
Output:
"none"
๐ก Note:
These sides cannot form a valid triangle because 1 + 1 = 2, which is not greater than 3. The triangle inequality is violated.
Constraints
-
nums.length == 3 -
1 โค nums[i] โค 109 - All sides are positive integers
Visualization
Tap to expand
Understanding the Visualization
1
Gather the Sticks
You have three sticks with lengths from the input array
2
Triangle Test
Check if any two sticks together are longer than the third stick
3
Count Matches
See how many sticks have the same length
4
Classify Result
Determine triangle type based on equal side count
Key Takeaway
๐ฏ Key Insight: Sort the sides first to optimize triangle inequality checking - you only need to verify that the sum of the two smaller sides exceeds the largest side!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code