Problem

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.

Examples

Example 1:

1
2
3
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:

1
2
3
4
5
6
7
8
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 == 3
  • 1 <= nums[i] <= 100

Solution

Method 1 - Mathematics

A triangle can be formed if the sum of any two sides is greater than the third side. Once we determine that the triangle can be formed, we can classify it based on the following definitions:

  • Equilateral: All three sides are equal.
  • Isosceles: Exactly two sides are equal.
  • Scalene: All sides are different.

If the array doesn’t meet the triangle inequality condition, the answer is "none".

Approach

  1. Check for Triangle Formation: Verify the triangle inequality:

    • nums[0] + nums[1] > nums[2]
    • nums[1] + nums[2] > nums[0]
    • nums[0] + nums[2] > nums[1]
  2. Classify the Triangle:

    • If all three sides are equal (nums[0] == nums[1] == nums[2]), the triangle is equilateral.
    • If exactly two sides are equal (e.g., nums[0] == nums[1] || nums[1] == nums[2] || nums[0] == nums[2]), the triangle is isosceles.
    • If no sides are equal (nums[0] != nums[1] && nums[1] != nums[2] && nums[0] != nums[2]), the triangle is scalene.
  3. Return "none" if the triangle inequality is invalid.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public String triangleType(int[] nums) {
        Arrays.sort(nums); // Sort to simplify triangle inequality checks.
        
        // Triangle inequality check
        if (nums[0] + nums[1] <= nums[2]) {
            return "none";
        }
        
        // Classification of triangle
        if (nums[0] == nums[1] && nums[1] == nums[2]) {
            return "equilateral";
        } else if (nums[0] == nums[1] || nums[1] == nums[2] || nums[0] == nums[2]) {
            return "isosceles";
        } else {
            return "scalene";
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution:
    def triangleType(self, nums: List[int]) -> str:
        nums.sort()  # Sort to simplify triangle inequality checks.
        
        # Triangle inequality check
        if nums[0] + nums[1] <= nums[2]:
            return "none"
        
        # Classification of triangle
        if nums[0] == nums[1] == nums[2]:
            return "equilateral"
        elif nums[0] == nums[1] or nums[1] == nums[2] or nums[0] == nums[2]:
            return "isosceles"
        else:
            return "scalene"

Complexity

  • ⏰ Time complexity: O(1) as the operations involve a fixed number of comparisons.
  • 🧺 Space complexity: O(1) as no extra space is used beyond a few primitive variables.