Skip to main content

Type of Triangle - Video Solutions

EasyArrayMathSorting

Type of Triangle | Simple Explanation | Leetcode 3024 | codestorywithMIK

codestorywithMIK
9:363,338 views
10 video solutions available

Type of Triangle - Video Solution

Watch 10 video solutions for Type of Triangle, a easy level problem involving Array, Math, Sorting. This walkthrough by codestorywithMIK has 3,338 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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
Read full problem with examples

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.

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