Type of Triangle
EasyPrompt
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.
- A triangle is called isosceles if it has exactly two sides of equal length.
- A triangle is called 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.
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] <= 100
Approaches
2 approaches with complexity analysis and trade-offs.
This approach directly translates the problem's definition into a series of conditional statements. It first validates if the three given lengths can form a triangle using the triangle inequality theorem. If they can, it then checks the conditions for equilateral, isosceles, and scalene triangles in a specific order to determine the correct type.
Algorithm
- Let the three side lengths be
a,b, andcfrom the input arraynums. - First, check if the given lengths can form a valid triangle using the triangle inequality theorem: the sum of the lengths of any two sides of a triangle must be greater than the length of the third side.
- This translates to three conditions:
a + b > c,a + c > b, andb + c > a. - If any of these conditions are false, the lengths cannot form a triangle, so we return
"none".
- This translates to three conditions:
- If the lengths form a valid triangle, proceed to classify it:
- Check if all three sides are equal (
a == b && b == c). If so, it's an"equilateral"triangle. - If not equilateral, check if exactly two sides are equal (
a == b || b == c || a == c). If so, it's an"isosceles"triangle. - If neither of the above is true, it means all sides are of different lengths, so it's a
"scalene"triangle.
- Check if all three sides are equal (
Walkthrough
The logic is implemented using a sequence of if-else if-else statements.
-
Triangle Validity Check: The primary and most crucial step is to ensure the sides can form a triangle. We check if
nums[0] + nums[1] > nums[2],nums[0] + nums[2] > nums[1], andnums[1] + nums[2] > nums[0]. If any of these checks fail (i.e., the sum is less than or equal to the third side), we immediately return"none". -
Type Classification: If the validity check passes, we determine the type.
- We first check for the most specific case: equilateral. If
nums[0],nums[1], andnums[2]are all equal, we return"equilateral". - Next, we check for the isosceles case. If any pair of sides is equal (
nums[0] == nums[1]ornums[1] == nums[2]ornums[0] == nums[2]), we return"isosceles". This check is performed after the equilateral check because an equilateral triangle also satisfies the isosceles condition, but the problem requires the most specific classification. - If the triangle is neither equilateral nor isosceles, it must be scalene, so we return
"scalene"as the default case for a valid triangle.
- We first check for the most specific case: equilateral. If
class Solution { public String triangleType(int[] nums) { int a = nums[0]; int b = nums[1]; int c = nums[2]; // Check for triangle inequality if (a + b <= c || a + c <= b || b + c <= a) { return "none"; } // Check for triangle type if (a == b && b == c) { return "equilateral"; } else if (a == b || b == c || a == c) { return "isosceles"; } else { return "scalene"; } }}Complexity
Time
O(1) - The number of comparisons and arithmetic operations is fixed, regardless of the values of the side lengths, because the input array size is always 3.
Space
O(1) - The amount of memory used is constant and does not depend on the input values, as we only use a few variables to hold the side lengths.
Trade-offs
Pros
Simple to understand as it directly follows the definitions provided in the problem statement.
Easy and quick to implement without any preliminary data transformation.
Cons
The triangle inequality check requires three separate comparisons (
a + b > c,a + c > b,b + c > a), which is slightly more verbose than necessary.
Solutions
Solution
public class Solution { public string TriangleType(int[] nums) { Array.Sort(nums); if (nums[0] + nums[1] <= nums[2]) { return "none"; } if (nums[0] == nums[2]) { return "equilateral"; } if (nums[0] == nums[1] || nums[1] == nums[2]) { return "isosceles"; } return "scalene"; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.