LeetCode Problem Workspace
Type of Triangle
Determine the type of triangle from a three-element array using side sums and equality checks efficiently in constant time.
3
Topics
6
Code langs
3
Related
Practice Focus
Easy · Array plus Math
Answer-first summary
Determine the type of triangle from a three-element array using side sums and equality checks efficiently in constant time.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array plus Math
Check if the three given sides satisfy the triangle inequality for all pairs. If not, return 'none'. Otherwise, compare the side lengths: if all are equal, return 'equilateral'; if exactly two are equal, return 'isosceles'; if all are distinct, return 'scalene'. This approach leverages direct array access and basic math operations, ensuring constant time evaluation for the triangle type.
Problem Statement
You are given an array nums of exactly three integers representing the lengths of potential triangle sides. Determine whether these lengths can form a valid triangle using the triangle inequality principle.
If a triangle can be formed, return a string indicating its type: 'equilateral' if all sides are equal, 'isosceles' if exactly two sides are equal, or 'scalene' if all sides differ. If the sides cannot form a triangle, return 'none'. Each number in nums is guaranteed to be between 1 and 100.
Examples
Example 1
Input: nums = [3,3,3]
Output: "equilateral"
Since all the sides are of equal length, therefore, it will form an equilateral triangle.
Example 2
Input: nums = [3,4,5]
Output: "scalene"
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 == 3
- 1 <= nums[i] <= 100
Solution Approach
Validate Triangle Inequality
Check that the sum of any two sides is greater than the third side. If any condition fails, immediately return 'none'. This prevents misclassifying invalid triangles.
Check for Equilateral Triangle
Compare all three sides for equality. If nums[0] == nums[1] == nums[2], return 'equilateral'. This directly handles the simplest equal-side case.
Differentiate Isosceles and Scalene
If exactly two sides are equal, return 'isosceles'. If all sides are distinct, return 'scalene'. Use conditional checks on the array elements to cover these final possibilities.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(1) |
| Space | O(1) |
Time complexity is O(1) since the array size is fixed at 3 and only a few comparisons are required. Space complexity is O(1) because no additional storage proportional to input is used.
What Interviewers Usually Probe
- Looking for immediate validation of triangle inequality.
- Expect concise conditional checks rather than loops.
- Interest in correctly distinguishing all triangle types without overcomplicating.
Common Pitfalls or Variants
Common pitfalls
- Forgetting the triangle inequality and returning a type for invalid sides.
- Confusing isosceles with equilateral when two sides are equal.
- Overcomplicating with unnecessary loops or sorting for only three elements.
Follow-up variants
- Determine the type of triangle when given sides in a larger array and checking every triplet.
- Return the perimeter along with the triangle type for valid triangles.
- Allow floating-point side lengths and handle precision in comparisons.
FAQ
What does the triangle inequality check entail for this problem?
For nums = [a,b,c], ensure a+b>c, a+c>b, and b+c>a. Failure of any means the triangle cannot exist.
How do I identify an equilateral triangle using array elements?
If nums[0] == nums[1] == nums[2], all sides are equal and the triangle is equilateral.
How is an isosceles triangle distinguished from scalene?
Check if exactly two sides are equal. If so, return 'isosceles'; if all sides differ, return 'scalene'.
Can this method handle negative or zero-length sides?
No, nums elements are constrained between 1 and 100, so all sides are positive integers.
What is the main pattern for solving 'Type of Triangle' on LeetCode?
The problem uses an array plus math pattern: direct array access with triangle inequality and side equality comparisons.
Solution
Solution 1: Sorting + Case Discussion
First, we sort the array, and then we can classify and discuss according to the definition of a triangle.
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"Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array plus Math
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward