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.
A more refined and elegant approach involves sorting the side lengths first. By sorting the array nums, the triangle inequality check is simplified to a single comparison. The subsequent classification of the triangle type also becomes slightly more streamlined.
Algorithm
- First, sort the input array
numsin non-decreasing order. - Let the sorted sides be
s1,s2, ands3(wheres1 <= s2 <= s3). - Check the simplified triangle inequality theorem. We only need to check if the sum of the two shorter sides is greater than the longest side:
s1 + s2 > s3.- If
s1 + s2 <= s3, it cannot form a triangle, so return"none".
- If
- If it's a valid triangle, classify it using the sorted sides:
- If
s1 == s3, all sides must be equal. Return"equilateral". - Else, if
s1 == s2ors2 == s3, exactly two sides are equal. Return"isosceles". - Otherwise, all sides are different. Return
"scalene".
- If
Walkthrough
This method leverages sorting to simplify the logical checks.
-
Sort: The
numsarray is sorted in ascending order. For an array of size 3, this is a trivial, constant-time operation. -
Simplified Triangle Validity Check: After sorting, let the sides be
s1, s2, s3. The triangle inequality theorem (a+b>c,a+c>b,b+c>a) simplifies to just one check:s1 + s2 > s3. This is becauses3is the longest side, so if the sum of the two shorter sides is greater than the longest, the other two conditions (s1 + s3 > s2ands2 + s3 > s1) will inherently be true. Ifs1 + s2 <= s3, we return"none". -
Type Classification: With sorted sides, classification is straightforward.
- Equilateral: If
s1 == s3, it impliess1 == s2 == s3because the array is sorted. Return"equilateral". - Isosceles: If it's not equilateral, we check if
s1 == s2ors2 == s3. If either is true, it's an isosceles triangle. Return"isosceles". - Scalene: If it's a valid triangle but is neither equilateral nor isosceles, it must be scalene. Return
"scalene".
- Equilateral: If
import java.util.Arrays; class Solution { public String triangleType(int[] nums) { Arrays.sort(nums); int s1 = nums[0]; int s2 = nums[1]; int s3 = nums[2]; // Check for triangle inequality (simplified) if (s1 + s2 <= s3) { return "none"; } // Check for triangle type if (s1 == s2 && s2 == s3) { return "equilateral"; } else if (s1 == s2 || s2 == s3) { return "isosceles"; } else { return "scalene"; } }}Complexity
Time
O(1) - Sorting an array of fixed size 3 takes a constant number of operations. The rest of the logic is also constant time.
Space
O(1) - `Arrays.sort()` in Java for primitive types uses an in-place quicksort, which has an average space complexity of O(log n) but is O(1) for a fixed size of 3. No significant extra space is used.
Trade-offs
Pros
More efficient and elegant due to the simplified triangle inequality check, which reduces three comparisons to one.
The logic is cleaner and less error-prone.
This pattern is more scalable and robust for similar geometric problems.
Cons
Requires an initial sorting step, which might be considered a slight overhead, although it's a constant-time operation for a fixed-size array of 3.
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"; }}class Solution {public String triangleType(int[] nums) { Arrays.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"; }}class Solution {public: string triangleType(vector<int> &nums) { sort(nums.begin(), nums.end()); 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"; }};class Solution: def triangleType(self, nums: List[int]) -> str: nums . sort() if nums[0] + nums[1] <= nums[2]: return "none" if nums[0] == nums[2]: return "equilateral" if nums[0] == nums[1] or 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.