Triangle Validator - Problem

Given three positive integers representing the lengths of three sides, determine if they can form a valid triangle. If they can form a valid triangle, classify it as:

  • Equilateral - All three sides are equal
  • Isosceles - Exactly two sides are equal
  • Scalene - All three sides are different

For three sides to form a valid triangle, they must satisfy the triangle inequality theorem: the sum of any two sides must be greater than the third side.

Input: Three positive integers a, b, and c representing side lengths.

Output: Return "Invalid" if they cannot form a triangle, otherwise return the triangle type as a string.

Input & Output

Example 1 — Valid Scalene Triangle
$ Input: a = 5, b = 12, c = 13
Output: Scalene
💡 Note: Triangle inequality: 5+12=17>13 ✓, 5+13=18>12 ✓, 12+13=25>5 ✓. All sides different, so it's Scalene.
Example 2 — Valid Isosceles Triangle
$ Input: a = 5, b = 5, c = 8
Output: Isosceles
💡 Note: Triangle inequality: 5+5=10>8 ✓, 5+8=13>5 ✓, 5+8=13>5 ✓. Two sides equal (a=b=5), so it's Isosceles.
Example 3 — Invalid Triangle
$ Input: a = 1, b = 2, c = 5
Output: Invalid
💡 Note: Triangle inequality fails: 1+2=3 is not greater than 5. Cannot form a valid triangle.

Constraints

  • 1 ≤ a, b, c ≤ 1000
  • All inputs are positive integers

Visualization

Tap to expand
Triangle ValidatorINPUTALGORITHMRESULT5side a12side b13side c1Check Triangle Inequalitya + b > c? → 5 + 12 > 13 ✓a + c > b? → 5 + 13 > 12 ✓b + c > a? → 12 + 13 > 5 ✓2Valid Triangle FoundAll inequality conditions satisfied3Classify Triangle Typea ≠ b ≠ c → All sides different4Return ClassificationTriangle type: ScaleneValid TriangleSCALENEAll sides differentProperties:• No equal sides• Most common type• Irregular shapeKey Insight:The triangle inequality theorem (sum of any two sides must exceed the third)is the fundamental rule that determines if three lengths can form a valid triangle.TutorialsPoint - Triangle Validator | Combined Validation and Classification
Asked in
Amazon 15 Microsoft 12 Google 8
23.5K Views
Medium Frequency
~8 min Avg. Time
890 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