Type of Triangle
Source: leetcode
Difficulty: Easy
Tags: array, math, sorting

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`




C++ solution:
class Solution {
public:
    string triangleType(vector<int>& nums) {
        
        if((nums[0]+nums[1])>nums[2] && (nums[0]+nums[2])>nums[1] 
            && (nums[1]+nums[2])   >nums[0] )
        {
        if(nums[0]==nums[1] && nums[1]==nums[2]) return "equilateral";
        if(nums[0]==nums[1] || nums[0]==nums[2] || nums[1]==nums[2])return "isosceles";
        if(nums[0]!=nums[1] && nums[0]!=nums[2] && nums[1]!=nums[2]) return "scalene";
        }
        return "none";
        
        
    }
};

Go solution:
func triangleType(nums []int) string {
    set := make(map[int]bool)
    count := 0
    sum := 0

    for _, n := range nums {
        sum += n
        if _, ok := set[n]; ok {
            count++
            continue
        }
        set[n] = true
    }

    for _, n := range nums {
        if n >= sum - n { return "none"}
    }

    if count == 2 { return  "equilateral" }
    if count == 1 { return "isosceles"}
    return "scalene"
}

Java solution:
class Solution {
    public String triangleType(int[] nums) {
        if(nums[0]+nums[1]>nums[2] && nums[1]+nums[2]>nums[0] && nums[2]+nums[0]>nums[1])
        {
            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 "scalene";
            }
            else if(nums[0]==nums[1] || nums[0]==nums[2] || nums[1]==nums[2])
            {
                return "isosceles";
            }
        }
        return "none";
    }
}

JavaScript solution:
var triangleType = function(nums) {
    [a,b,c]= nums;
    if((a+b)<=c || (b+c) <= a || (c+a) <= b) return 'none'
    else if(a===b && b===c && c===a) return 'equilateral'
    else if(a !== b && b !== c && c !== a) return 'scalene'
    else return 'isosceles'
};

Python solution:
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"

Ruby solution:
# @param {Integer[]} nums
# @return {String}
def triangle_type(nums)

    nums.sort!
    # a + b > c is a rule for triangles to be valid.
    if nums[0] + nums[1] <= nums[2]
        return "none"
    end

    freq = Hash.new(0)
    max_freq = 0
    nums.each do |num|
        freq[num] += 1
        max_freq = [max_freq, freq[num]].max
    end

    case max_freq
    when 3
        return "equilateral"
    when 2
        return "isosceles"
    when 1
        return "scalene"
    end    
end