You are given a 0-indexed integer array nums of size 3 which can form the sides of a triangle.
Return a string representing the type of triangle that can be formed or "none" if it cannot form a triangle.
Example 1:
Input: nums = [3,3,3] Output: "equilateral" Explanation: Since all the sides are of equal length, therefore, it will form an equilateral triangle.
Example 2:
Input: nums = [3,4,5] Output: "scalene" Explanation: nums[0] + nums[1] = 3 + 4 = 7, which is greater than nums[2] = 5. nums[0] + nums[2] = 3 + 5 = 8, which is greater than nums[1] = 4. nums[1] + nums[2] = 4 + 5 = 9, which is greater than nums[0] = 3. Since the sum of the two sides is greater than the third side for all three cases, therefore, it can form a triangle. As all the sides are of different lengths, it will form a scalene triangle.
Constraints:
nums.length == 31 <= nums[i] <= 100Problem Overview: You receive an array of three integers representing side lengths. The task is to determine whether these sides form a valid triangle and classify it as equilateral, isosceles, scalene, or none. The key requirement is validating the triangle first using the triangle inequality theorem.
Approach 1: Triangle Inequality Theorem and Side Length Comparison (Time: O(1), Space: O(1))
Start by validating whether the three sides can actually form a triangle. According to the triangle inequality theorem, the sum of any two sides must be greater than the third. With only three values, you can directly check all three conditions: a + b > c, a + c > b, and b + c > a. If any of these fail, the triangle does not exist and the answer is "none".
Once validity is confirmed, classify the triangle using simple comparisons. If all three sides are equal, return "equilateral". If exactly two sides match, return "isosceles". If all sides differ, return "scalene". This approach uses straightforward math checks and constant-time comparisons, making it the simplest and most direct solution.
Approach 2: Sorting and Triangle Type Classification (Time: O(1), Space: O(1))
Another clean strategy is to sort the three side lengths first using a sorting step. After sorting, the largest side will always be the last element. This simplifies the triangle validity check because you only need to verify one condition: a + b > c. If this fails, the sides cannot form a triangle.
Sorting also makes classification easier because equal values become adjacent. If a == b == c, the triangle is equilateral. If only two adjacent sides match, it is isosceles. Otherwise, it is scalene. While sorting is technically unnecessary for just three elements, it produces cleaner logic and mirrors how triangle checks are often implemented in geometry-related problems.
Recommended for interviews: The triangle inequality approach is usually preferred because it demonstrates understanding of the underlying geometric rule and avoids unnecessary operations. Interviewers expect you to quickly validate the triangle and classify it with simple comparisons. The sorting approach is also acceptable and sometimes easier to reason about, especially when generalizing the logic to larger inputs. Showing the direct mathematical check first and mentioning sorting as an alternative demonstrates both problem-solving clarity and practical coding judgment.
This approach involves checking if the given sides can form a triangle using the triangle inequality theorem. If they can form a triangle, we check the equality of the side lengths to determine the type of triangle. The triangle inequality requires that the sum of the lengths of any two sides must be greater than the length of the remaining side. Once we establish that the sides form a valid triangle, we compare the sides to classify the triangle as equilateral, isosceles, or scalene.
Here's how we can implement this:
In this C solution, we first check whether the inputs satisfy the triangle inequality theorem using a series of conditional checks. If any two sides do not sum to be greater than the third side, we return "none". Otherwise, we then check for the equality of sides to find out if the triangle is equilateral, isosceles, or scalene.
Time Complexity: O(1), since we are performing a fixed number of operations.
Space Complexity: O(1), as no additional data structures are used.
This approach involves sorting the sides of the triangle first to simplify the comparison process. Sorting will guarantee that a <= b <= c, allowing us to simply verify the triangle inequality with a + b > c. Once sorted, determining the type of triangle becomes straightforward with equality checks.
Steps involved:
a <= b <= c.a + b > c.We employ the qsort function to sort the array in ascending order. This simplifies the inequality checking to a single condition: if nums[0] + nums[1] > nums[2] holds true, a triangle is possible. Side equality checks are performed on the sorted array to determine the triangle type, leveraging the sorted order for simplicity.
Time Complexity: O(1), sorting a constant-size array.
Space Complexity: O(1), no additional data structures are used beyond the input array.
First, we sort the array, and then we can classify and discuss according to the definition of a triangle.
The time complexity is O(1), and the space complexity is O(1).
| Approach | Complexity |
|---|---|
| Using Triangle Inequality Theorem and Side Length Comparison | Time Complexity: O(1), since we are performing a fixed number of operations. |
| Using Sorting and Triangle Type Classification | Time Complexity: O(1), sorting a constant-size array. |
| Sorting + Case Discussion | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Triangle Inequality + Side Comparison | O(1) | O(1) | Best general solution when checking triangle validity and type directly |
| Sorting + Classification | O(1) | O(1) | Cleaner logic when sorting simplifies comparisons |
Type of Triangle | Simple Explanation | Leetcode 3024 | codestorywithMIK • codestorywithMIK • 3,234 views views
Watch 9 more video solutions →Practice Type of Triangle with our built-in code editor and test cases.
Practice on FleetCode