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] <= 100This 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.
C++
Java
Python
C#
JavaScript
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.
C++
Java
Python
C#
JavaScript
Time Complexity: O(1), sorting a constant-size array.
Space Complexity: O(1), no additional data structures are used beyond the input array.
| 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. |
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,285 views views
Watch 9 more video solutions →Practice Type of Triangle with our built-in code editor and test cases.
Practice on FleetCode