Sitemap
Photo by Chris Ried on Unsplash

LeetCode day315–3024

2 min readMay 19, 2025

--

Everyday has one random leetcode quiz. Today’s quiz is 3024. Type of Triangle

Description:

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.

Get Jimmy’s stories in your inbox

Join Medium for free to get updates from this writer.

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.

Given three side lengths aaa, bbb, and ccc, they can form a triangle if and only if the following three conditions are all satisfied

  • A + B > C
  • A + C > B
  • B + C > A

The time complexity is O(1)

/**
* @param {number[]} nums
* @return {string}
*/
var triangleType = function (nums) {
const isTriangle = nums[0] + nums[1] > nums[2]
&& nums[1] + nums[2] > nums[0]
&& nums[2] + nums[0] > nums[1]

if (!isTriangle) {
return 'none'
}

if (nums[0] === nums[1] && nums[1] === nums[2]) {
return 'equilateral'
} else if (nums[0] !== nums[1] && nums[0] !== nums[2] && nums[1] !== nums[2]) {
return 'scalene'
}

return 'isosceles'
};

--

--

Jimmy
Jimmy

Written by Jimmy

The frontend enginer is learning