# Type of Triangle
**Difficulty:** EASY
[External](https://leetcode.com/problems/type-of-triangle)
Canonical: https://scaleengineer.com/dsa/problems/type-of-triangle
**Patterns:** [Math](https://scaleengineer.com/dsa/patterns/math)
**Algorithms:** [Sorting](https://scaleengineer.com/algorithms/sorting)
**Data structures:** Array
**Companies:** [IBM](https://scaleengineer.com/companies/ibm)
---
## 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._

**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 == 3`
* `1 <= nums[i] <= 100`

# Approaches
## Direct Comparison using If-Else
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.
**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.
**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.
### Explanation
The logic is implemented using a sequence of `if-else if-else` statements. 

1.  **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]`, and `nums[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"`.

2.  **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]`, and `nums[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]` or `nums[1] == nums[2]` or `nums[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.

```java
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";
        }
    }
}
```
### Algorithm
- Let the three side lengths be `a`, `b`, and `c` from the input array `nums`.
- 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`, and `b + c > a`.
  - If any of these conditions are false, the lengths cannot form a triangle, so we return `"none"`.
- 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.

## Sorting for Simplified Checks
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.
**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.
**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.
### Explanation
This method leverages sorting to simplify the logical checks.

1.  **Sort**: The `nums` array is sorted in ascending order. For an array of size 3, this is a trivial, constant-time operation.

2.  **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 because `s3` is the longest side, so if the sum of the two shorter sides is greater than the longest, the other two conditions (`s1 + s3 > s2` and `s2 + s3 > s1`) will inherently be true. If `s1 + s2 <= s3`, we return `"none"`.

3.  **Type Classification**: With sorted sides, classification is straightforward.
    - **Equilateral**: If `s1 == s3`, it implies `s1 == s2 == s3` because the array is sorted. Return `"equilateral"`.
    - **Isosceles**: If it's not equilateral, we check if `s1 == s2` or `s2 == 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"`.

```java
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";
        }
    }
}
```
### Algorithm
- First, sort the input array `nums` in non-decreasing order.
- Let the sorted sides be `s1`, `s2`, and `s3` (where `s1 <= 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 it's a valid triangle, classify it using the sorted sides:
  - If `s1 == s3`, all sides must be equal. Return `"equilateral"`.
  - Else, if `s1 == s2` or `s2 == s3`, exactly two sides are equal. Return `"isosceles"`.
  - Otherwise, all sides are different. Return `"scalene"`.

# Solutions
### CSharp

```csharp
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";
    }
}
```

### Java

```java
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";
  }
}

```

### CPP

```cpp
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";
  }
};

```

### Python

```python
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"

```
