Type of Triangle solution and explanation
9 more videos on this problem
Type of Triangle | Simple Explanation | Leetcode 3024 | codestorywithMIKcodestorywithMIK · 9:36 · 3K views
3024.Type Of Triangle #Leetcode#Daily ChallengeByte Bridge · 7:48 · 467 views
3024. Type of Triangle | Leetcode Daily - PythonLeetcode Daily · 6:55 · 227 views
3024. Type of Triangle | O(1) | LeetcodeRapid Syntax · 6:06 · 129 views
Leetcode 3024. Types of Triangle || Java || Today’s ContestExpertFunda · 9:37 · 112 views
LeetCode 3024 | Type of Triangle | Easy Problem | C++ SolutionVishwa Patel · 4:11 · 101 views
Type of Triangle | LeetCode 3024 | Java Code | Developer CoderDeveloper Coder · 8:08 · 95 views
LeetCode 3024 - Type Of Triangle - JavaAlpha-Code · 5:59 · 90 views
LeetCode 3024 – Type of Triangle – Easy Solution Explained&BEYOND · 7:23 · 63 views
3024. Type of Triangle || Leetcode Daily Challenge || potd || mathsEye on AI and DSA · 4:17 · 54 views
The problem
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 == 31 <= 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'.
def triangle_type(nums):
a, b, c = nums
if a + b <= c or a + c <= b or b + c <= a:
return "none"
if a == b == c:
return "equilateral"
if a == b or b == c or a == c:
return "isosceles"
return "scalene"
nums = [3, 4, 5]
print(triangle_type(nums))public class TriangleType {
public static String triangleType(int[] nums) {
int a = nums[0], b = nums[1], c = nums[2];
if (a + b <= c || a + c <= b || b + c <= a) {
return "none";
}
if (a == b && b == c) {
return "equilateral";
}
if (a == b || b == c || a == c) {
return "isosceles";
}
return "scalene";
}
public static void main(String[] args) {
int[] nums = {3, 4, 5};
System.out.println(triangleType(nums));
}
}#include <iostream>
#include <vector>
std::string triangleType(std::vector<int>& nums) {
int a = nums[0], b = nums[1], c = nums[2];
if (a + b <= c || a + c <= b || b + c <= a) {
return "none";
}
if (a == b && b == c) {
return "equilateral";
}
if (a == b || b == c || a == c) {
return "isosceles";
}
return "scalene";
}
int main() {
std::vector<int> nums = {3, 4, 5};
std::cout << triangleType(nums) << std::endl;
return 0;
}function triangleType(nums) {
const [a, b, c] = nums;
if (a + b <= c || a + c <= b || b + c <= a) {
return "none";
}
if (a === b && b === c) {
return "equilateral";
}
if (a === b || b === c || a === c) {
return "isosceles";
}
return "scalene";
}
const nums = [3, 4, 5];
console.log(triangleType(nums));#include <stdio.h>
const char* triangleType(int* nums) {
int a = nums[0], b = nums[1], c = nums[2];
if (a + b <= c || a + c <= b || b + c <= a) {
return "none";
}
if (a == b && b == c) {
return "equilateral";
}
if (a == b || b == c || a == c) {
return "isosceles";
}
return "scalene";
}
int main() {
int nums[3] = {3, 4, 5};
printf("%s\n", triangleType(nums));
return 0;
}using System;
public class TriangleType {
public static string GetTriangleType(int[] nums) {
int a = nums[0], b = nums[1], c = nums[2];
if (a + b <= c || a + c <= b || b + c <= a) {
return "none";
}
if (a == b && b == c) {
return "equilateral";
}
if (a == b || b == c || a == c) {
return "isosceles";
}
return "scalene";
}
public static void Main(string[] args) {
int[] nums = new[] {3, 4, 5};
Console.WriteLine(GetTriangleType(nums));
}
}In this Python implementation, we directly unpack the list into variables a, b, and c for better readability. The triangle inequality check is performed first, followed by checks on side equality to classify the triangle type. The if-elif structure makes the code concise.
Time Complexity: O(1), performing constant-time checks and operations.
Space Complexity: O(1), due to single-variable assignments and minimal space usage.
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.
def triangle_type(nums):
nums.sort()
a, b, c = nums
if a + b <= c:
return "none"
if a == c:
return "equilateral"
if a == b or b == c:
return "isosceles"
return "scalene"
nums = [3, 4, 5]
print(triangle_type(nums))import java.util.Arrays;
public class TriangleType {
public static String triangleType(int[] nums) {
Arrays.sort(nums);
int a = nums[0], b = nums[1], c = nums[2];
if (a + b <= c) {
return "none";
}
if (a == c) {
return "equilateral";
}
if (a == b || b == c) {
return "isosceles";
}
return "scalene";
}
public static void main(String[] args) {
int[] nums = {3, 4, 5};
System.out.println(triangleType(nums));
}
}#include <iostream>
#include <vector>
#include <algorithm>
std::string triangleType(std::vector<int>& nums) {
std::sort(nums.begin(), nums.end());
int a = nums[0], b = nums[1], c = nums[2];
if (a + b <= c) {
return "none";
}
if (a == c) {
return "equilateral";
}
if (a == b || b == c) {
return "isosceles";
}
return "scalene";
}
int main() {
std::vector<int> nums = {3, 4, 5};
std::cout << triangleType(nums) << std::endl;
return 0;
}function triangleType(nums) {
nums.sort((a, b) => a - b);
const [a, b, c] = nums;
if (a + b <= c) {
return "none";
}
if (a === c) {
return "equilateral";
}
if (a === b || b === c) {
return "isosceles";
}
return "scalene";
}
const nums = [3, 4, 5];
console.log(triangleType(nums));#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
const char* triangleType(int* nums) {
qsort(nums, 3, sizeof(int), compare);
int a = nums[0], b = nums[1], c = nums[2];
if (a + b <= c) {
return "none";
}
if (a == c) {
return "equilateral";
}
if (a == b || b == c) {
return "isosceles";
}
return "scalene";
}
int main() {
int nums[3] = {3, 4, 5};
printf("%s\n", triangleType(nums));
return 0;
}using System;
using System.Linq;
public class TriangleType {
public static string GetTriangleType(int[] nums) {
Array.Sort(nums);
int a = nums[0], b = nums[1], c = nums[2];
if (a + b <= c) {
return "none";
}
if (a == c) {
return "equilateral";
}
if (a == b || b == c) {
return "isosceles";
}
return "scalene";
}
public static void Main(string[] args) {
int[] nums = new[] {3, 4, 5};
Console.WriteLine(GetTriangleType(nums));
}
}Python's built-in sort is used to streamline checks. After sorting, we perform a simple comparison for the triangle inequality and check equality of the sides in an easily readable manner. Sorting aids in reduction of checks needed for inequality.
Time Complexity: O(1), since the sort is over 3 fixed elements.
Space Complexity: O(1), as no additional space is required besides the input list.
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).
function triangleType(nums: number[]): string {
nums.sort((a, b) => a - b);
if (nums[0] + nums[1] <= nums[2]) {
return 'none';
}
if (nums[0] === nums[2]) {
return 'equilateral';
}
if (nums[0] === nums[1] || nums[1] === nums[2]) {
return 'isosceles';
}
return 'scalene';
}func triangleType(nums []int) string {
sort.Ints(nums)
if nums[0]+nums[1] <= nums[2] {
return "none"
}
if nums[0] == nums[2] {
return "equilateral"
}
if nums[0] == nums[1] || nums[1] == nums[2] {
return "isosceles"
}
return "scalene"
}Complexity comparison
| Approach | Complexity |
|---|---|
| Using Triangle Inequality Theorem and Side Length Comparison | Time Complexity: O(1), performing constant-time checks and operations. |
| Using Sorting and Triangle Type Classification | Time Complexity: O(1), since the sort is over 3 fixed elements. |
| Sorting + Case Discussion | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Triangle Inequality + Side Comparison | O(1) | O(1) | Best general solution when checking triangle validity and type directly |
| Sorting + Classification | O(1) | O(1) | Cleaner logic when sorting simplifies comparisons |
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.
