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
Triangle Classification JourneyStep 1GatherSticks553Step 2TriangleTest5 + 5 > 3? โœ“5 + 3 > 5? โœ“5 + 3 > 5? โœ“Step 3CountMatches5 == 5? โœ“5 == 3? โœ—2 equal sidesStep 4ClassifyResultISOSCELESTriangle Types ReferenceEEquilateral: All 3 sides equalExample: [5, 5, 5]IIsosceles: Exactly 2 sides equalExample: [5, 5, 3]SScalene: All sides differentExample: [3, 4, 5]โœ—None: Triangle inequality violated
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!
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
28.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen