Skip to main content

Type of Triangle - Solution & Explanation

EasyArrayMathSorting20 min readAsked at: Microsoft, Meta, IBM +2
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: You receive an array of three integers representing side lengths. The task is to determine whether these sides form a valid triangle and classify it as equilateral, isosceles, scalene, or none. The key requirement is validating the triangle first using the triangle inequality theorem.

Approach 1: Triangle Inequality Theorem and Side Length Comparison (Time: O(1), Space: O(1))

Start by validating whether the three sides can actually form a triangle. According to the triangle inequality theorem, the sum of any two sides must be greater than the third. With only three values, you can directly check all three conditions: a + b > c, a + c > b, and b + c > a. If any of these fail, the triangle does not exist and the answer is "none".

Once validity is confirmed, classify the triangle using simple comparisons. If all three sides are equal, return "equilateral". If exactly two sides match, return "isosceles". If all sides differ, return "scalene". This approach uses straightforward math checks and constant-time comparisons, making it the simplest and most direct solution.

Approach 2: Sorting and Triangle Type Classification (Time: O(1), Space: O(1))

Another clean strategy is to sort the three side lengths first using a sorting step. After sorting, the largest side will always be the last element. This simplifies the triangle validity check because you only need to verify one condition: a + b > c. If this fails, the sides cannot form a triangle.

Sorting also makes classification easier because equal values become adjacent. If a == b == c, the triangle is equilateral. If only two adjacent sides match, it is isosceles. Otherwise, it is scalene. While sorting is technically unnecessary for just three elements, it produces cleaner logic and mirrors how triangle checks are often implemented in geometry-related problems.

Recommended for interviews: The triangle inequality approach is usually preferred because it demonstrates understanding of the underlying geometric rule and avoids unnecessary operations. Interviewers expect you to quickly validate the triangle and classify it with simple comparisons. The sorting approach is also acceptable and sometimes easier to reason about, especially when generalizing the logic to larger inputs. Showing the direct mathematical check first and mentioning sorting as an alternative demonstrates both problem-solving clarity and practical coding judgment.

Approach 1: Using Triangle Inequality Theorem and Side Length Comparison

This approach involves checking if the given sides can form a triangle using the triangle inequality theorem. If they can form a triangle, we check the equality of the side lengths to determine the type of triangle. The triangle inequality requires that the sum of the lengths of any two sides must be greater than the length of the remaining side. Once we establish that the sides form a valid triangle, we compare the sides to classify the triangle as equilateral, isosceles, or scalene.

Here's how we can implement this:

  • Check the triangle inequality theorem: If any sum of two sides is not greater than the third, it's not a triangle.
  • Check side lengths: If all sides are equal, return 'equilateral'. If two sides are equal, return 'isosceles'. Otherwise, return 'scalene'.

In this C solution, we first check whether the inputs satisfy the triangle inequality theorem using a series of conditional checks. If any two sides do not sum to be greater than the third side, we return "none". Otherwise, we then check for the equality of sides to find out if the triangle is equilateral, isosceles, or scalene.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), since we are performing a fixed number of operations.
Space Complexity: O(1), as no additional data structures are used.

Try this approach in the editor →

Approach 2: Using Sorting and Triangle Type Classification

This approach involves sorting the sides of the triangle first to simplify the comparison process. Sorting will guarantee that a <= b <= c, allowing us to simply verify the triangle inequality with a + b > c. Once sorted, determining the type of triangle becomes straightforward with equality checks.

Steps involved:

  • Sort the sides, ensuring a <= b <= c.
  • Apply a simplified triangle inequality check using a + b > c.
  • Classify the type of triangle by comparing the sorted sides.

We employ the qsort function to sort the array in ascending order. This simplifies the inequality checking to a single condition: if nums[0] + nums[1] > nums[2] holds true, a triangle is possible. Side equality checks are performed on the sorted array to determine the triangle type, leveraging the sorted order for simplicity.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), sorting a constant-size array.
Space Complexity: O(1), no additional data structures are used beyond the input array.

Try this approach in the editor →

Approach 3: Sorting + Case Discussion

First, we sort the array, and then we can classify and discuss according to the definition of a triangle.

  • If the sum of the smallest two numbers is less than or equal to the largest number, then it cannot form a triangle, return "none".
  • If the smallest number is equal to the largest number, then it is an equilateral triangle, return "equilateral".
  • If the smallest number is equal to the middle number or the middle number is equal to the largest number, then it is an isosceles triangle, return "isosceles".
  • Otherwise, return "scalene".

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using Triangle Inequality Theorem and Side Length Comparison

Time Complexity: O(1), since we are performing a fixed number of operations.
Space Complexity: O(1), as no additional data structures are used.

Using Sorting and Triangle Type Classification

Time Complexity: O(1), sorting a constant-size array.
Space Complexity: O(1), no additional data structures are used beyond the input array.

Sorting + Case Discussion—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Triangle Inequality + Side ComparisonO(1)O(1)Best general solution when checking triangle validity and type directly
Sorting + ClassificationO(1)O(1)Cleaner logic when sorting simplifies comparisons

Video Solution

Type of Triangle | Simple Explanation | Leetcode 3024 | codestorywithMIK • codestorywithMIK • 3,338 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Type of Triangle easy or hard?
Type of Triangle is considered an Easy problem on LeetCode with an acceptance rate around 44%. The challenge focuses on correctly applying the triangle inequality rule and handling the classification logic without missing edge cases.
Type of Triangle Python/Java solution
The implementation checks triangle validity using the triangle inequality theorem and then compares the three side values. The same logic works across Python, Java, C++, and JavaScript because it only requires arithmetic checks and simple conditional statements.
How to solve Type of Triangle in O(1)?
Check the triangle inequality conditions for the three sides. If any pair of sides does not sum to more than the third side, return "none". Otherwise compare the side values: all equal means equilateral, two equal means isosceles, and all different means scalene. Since there are only three numbers, the solution executes in constant time.
What is the best approach for Type of Triangle?
The triangle inequality theorem with direct side comparison is the best approach. First verify that the sum of any two sides is greater than the third. After confirming validity, classify the triangle using equality checks between the three sides. This solution runs in O(1) time and O(1) space.
Is Type of Triangle asked at Google/Amazon/Meta?
Problems involving triangle validity and classification appear frequently in coding interviews as basic geometry or array logic exercises. While this exact LeetCode problem may not appear directly, similar triangle inequality checks and classification logic are common screening questions.
What data structure is used in Type of Triangle?
The problem primarily uses an array of three integers representing the side lengths. No advanced data structures are required. The logic relies on arithmetic comparisons and optional sorting.
What is the time complexity of Type of Triangle?
The time complexity is O(1) because the input always contains exactly three side lengths. Only a fixed number of comparisons and arithmetic operations are required. Space complexity is also O(1) since no additional data structures are needed.

Ready to solve this problem?

Practice Type of Triangle with our built-in code editor and test cases.

Practice on FleetCode