Type of Triangle

Easy
1077.

Type of Triangle

Type of Triangle is an easy coding problem on arrays, math and sorting, asked in Amazon, Google and Infosys interviews.

nums holds the three side lengths of a candidate triangle. Return:

  • "equilateral" if all three sides are equal;
  • "isosceles" if exactly two are equal;
  • "scalene" if all three differ;
  • "none" if the sides cannot form a triangle at all.

Example 1

Input
nums = [3,3,3]
Output
equilateral

Example 2

Input
nums = [3,4,5]
Output
scalene

Example 3

Input
nums = [1,1,3]
Output
none
Explanation
1 + 1 is not greater than 3.

Constraints

  • nums.length == 3
  • 1 <= nums[i] <= 100
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @return {string}
 */
var triangleType = function(nums) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Type of Triangle — Solution in Python, Java & C++

Type of Triangle solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on arrays and math.

Type of Triangle is an easy coding problem on arrays, math and sorting, asked in Amazon, Google and Infosys interviews.

  • Difficulty: Easy
  • Topics: Arrays, Math, Sorting
  • Asked at: Amazon, Google, Infosys
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

nums holds the three side lengths of a candidate triangle. Return:

  • "equilateral" if all three sides are equal;
  • "isosceles" if exactly two are equal;
  • "scalene" if all three differ;
  • "none" if the sides cannot form a triangle at all.

Example 1

Input: nums = [3,3,3]
Output: equilateral

Example 2

Input: nums = [3,4,5]
Output: scalene

Example 3

Input: nums = [1,1,3]
Output: none
Explanation: 1 + 1 is not greater than 3.

Constraints

  • nums.length == 3
  • 1 <= nums[i] <= 100

Hints for Type of Triangle

  1. Hint 1

    Check validity first: the two shorter sides must sum to strictly more than the longest.

  2. Hint 2

    Sorting makes that a single comparison.

  3. Hint 3

    Then count the equal sides.

How to solve Type of Triangle

Sort the three sides. The triangle inequality then reduces to a + b > c for the sorted sides; if it fails the answer is "none". Otherwise classify by how many sides are equal.

Approach

  1. Sort the three values ascending.
  2. Return "none" if s[0] + s[1] <= s[2].
  3. Return "equilateral" if all three match, "isosceles" if one adjacent pair matches, else "scalene".

Why it works

Sorting collapses three triangle-inequality checks into one — the other two hold automatically once the longest side is identified. And the validity test must come first: [1,1,3] has two equal sides but is not a triangle, so classifying before checking would wrongly call it isosceles.

Complexity

  • Time — O(1)
  • Space — O(1)

Pitfalls

  • The inequality is strict: 1 + 2 = 3 is degenerate and counts as "none".
  • Validity is checked before the equal-sides classification.
  • After sorting, equality only needs to be tested between adjacent sides.

Reference solution

Python

from typing import List

def triangleType(nums: List[int]) -> str:
    s = sorted(nums)
    if s[0] + s[1] <= s[2]:
        return "none"
    if s[0] == s[1] == s[2]:
        return "equilateral"
    if s[0] == s[1] or s[1] == s[2]:
        return "isosceles"
    return "scalene"

Java

public static String triangleType(int[] nums) {
    int[] s = nums.clone();
    Arrays.sort(s);
    if (s[0] + s[1] <= s[2]) return "none";
    if (s[0] == s[1] && s[1] == s[2]) return "equilateral";
    if (s[0] == s[1] || s[1] == s[2]) return "isosceles";
    return "scalene";
}

C++

string triangleType(vector<int>& nums) {
    vector<int> s = nums;
    sort(s.begin(), s.end());
    if (s[0] + s[1] <= s[2]) return "none";
    if (s[0] == s[1] && s[1] == s[2]) return "equilateral";
    if (s[0] == s[1] || s[1] == s[2]) return "isosceles";
    return "scalene";
}

JavaScript

var triangleType = function(nums) {
    var s = nums.slice();
    s.sort(function(a, b) { return a - b; });
    if (s[0] + s[1] <= s[2]) return "none";
    if (s[0] === s[1] && s[1] === s[2]) return "equilateral";
    if (s[0] === s[1] || s[1] === s[2]) return "isosceles";
    return "scalene";
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Sorting Algorithms