Type of Triangle - Problem

You are given a 0-indexed integer array nums of size 3 which can form the sides of a triangle.

A triangle is called:

  • Equilateral if it has all sides of equal length
  • Isosceles if it has exactly two sides of equal length
  • Scalene if all its sides are of different lengths

Return a string representing the type of triangle that can be formed or "none" if it cannot form a triangle.

Input & Output

Example 1 — Equilateral Triangle
$ Input: nums = [3,3,3]
Output: "equilateral"
💡 Note: All three sides are equal (3 = 3 = 3), and they satisfy triangle inequality (3+3 > 3), so it forms an equilateral triangle.
Example 2 — Isosceles Triangle
$ Input: nums = [3,4,3]
Output: "isosceles"
💡 Note: Two sides are equal (3 = 3), and triangle inequality holds (3+3 > 4), so it forms an isosceles triangle.
Example 3 — Invalid Triangle
$ Input: nums = [1,2,3]
Output: "none"
💡 Note: Triangle inequality fails: 1+2 = 3, which is not greater than 3, so these sides cannot form a triangle.

Constraints

  • nums.length == 3
  • 1 ≤ nums[i] ≤ 109

Visualization

Tap to expand
Type of Triangle - Optimal Solution INPUT Integer Array (size 3) 3 nums[0] 3 nums[1] 3 nums[2] Triangle Sides: a=3 b=3 c=3 nums = [3, 3, 3] ALGORITHM STEPS 1 Check Valid Triangle a+b>c, b+c>a, a+c>b 3+3>3 OK | All pass! 2 Check Equilateral All sides equal: a==b==c 3==3==3 ? YES! 3 Check Isosceles Exactly 2 equal sides (skipped - found equilateral) 4 Check Scalene All sides different (skipped - found equilateral) Decision Order: 1. Valid --> 2. Equilateral 3. Isosceles --> 4. Scalene FINAL RESULT = = = All 3 sides are equal "equilateral" Verification: 3 = 3 = 3 [OK] Valid Equilateral Triangle Key Insight: The Triangle Inequality Theorem must be checked first: sum of any two sides must be greater than the third side. Then check in order: equilateral (all equal) --> isosceles (two equal) --> scalene (all different). Time Complexity: O(1) | Space Complexity: O(1) TutorialsPoint - Type of Triangle | Optimal Solution
Asked in
Amazon 15 Microsoft 12
12.5K Views
Medium Frequency
~8 min Avg. Time
420 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