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] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to determining the type of triangle involves checking every possible combination of side lengths against all triangle type conditions. It's like testing every single possibility to see if it fits the rules. We need to check against what defines an equilateral, isosceles, or scalene triangle.
Here's how the algorithm would work step-by-step:
def type_of_triangle(side_one, side_two, side_three):
# Check if all sides are equal; then it's equilateral
if side_one == side_two == side_three:
return "equilateral"
# If not equilateral, check if any two sides are equal; then it's isosceles
if side_one == side_two or side_one == side_three or side_two == side_three:
return "isosceles"
# If none of the above are true, then no sides are equal; scalene
return "scalene"To determine the type of triangle, we need to analyze the lengths of its sides. The most efficient approach involves sorting the side lengths first, and then applying the Pythagorean theorem and checking for equality to determine the triangle type.
Here's how the algorithm would work step-by-step:
def type_of_triangle(side_one, side_two, side_three):
sides = sorted([side_one, side_two, side_three])
smallest_side = sides[0]
middle_side = sides[1]
largest_side = sides[2]
if smallest_side + middle_side <= largest_side:
return "Not a triangle"
# Determine if it is a right, acute, or obtuse triangle
if largest_side**2 == smallest_side**2 + middle_side**2:
triangle_type = "Right"
elif largest_side**2 < smallest_side**2 + middle_side**2:
triangle_type = "Acute"
else:
triangle_type = "Obtuse"
# Determine if it is equilateral, isosceles, or scalene.
if side_one == side_two == side_three:
side_type = "Equilateral"
elif side_one == side_two or side_one == side_three or side_two == side_three:
side_type = "Isosceles"
else:
side_type = "Scalene"
# Combine the two classifications
if side_type == "Equilateral":
return "Equilateral"
else:
return triangle_type + " " + side_type| Case | How to Handle |
|---|---|
| One or more sides are zero | Treat zero as invalid side length and return 'Not a triangle'. |
| One or more sides are negative | Treat negative as invalid side length and return 'Not a triangle'. |
| Input sides violate triangle inequality (a + b <= c) | Return 'Not a triangle' if the sum of any two sides is not greater than the third side. |
| Sides are extremely large numbers leading to potential overflow during calculations | Use appropriate data types (e.g., long) to avoid integer overflow when calculating side lengths or comparison values, or use a different approach if overflow is still a concern. |
| Sides are floating-point numbers with potential precision issues | Use a small tolerance value (epsilon) when comparing floating-point numbers to account for precision errors. |
| All three sides are equal | Return 'Equilateral' since all sides have same length. |
| Two sides are equal | Check if two sides are equal, then check if it's also equilateral, return 'Isosceles'. |
| Sides are provided in an unexpected order or format | Sort the sides in ascending order for consistent comparison and easier triangle validity check. |