DDSA Solutions

3024. Type of Triangle

Time: O(1)
Space: O(1)

Problem Overview

Given three stick lengths, they form a non-zero area triangle iff the sum of the two smaller sides exceeds the largest.

Advertisement

Intuition

Given three stick lengths, they form a non-zero area triangle iff the sum of the two smaller sides exceeds the largest. Sort three numbers and check nums[0]+nums[1] > nums[2].

Algorithm

  1. 1Sort the three sides ascending.
  2. 2If nums[0] + nums[1] > nums[2], return the triangle type required (e.g. "yes" / specific label).
  3. 3Else degenerate or impossible triangle.

Example Walkthrough

Input: nums = [2,3,4]

  1. 1.Sorted: 2,3,4. 2+3=5 > 4 — valid triangle.

Output: valid triangle classification

Common Pitfalls

  • After sorting, only one inequality check needed.
  • Equality (2+3=5) is degenerate — not a valid triangle for positive area.
  • Exactly three sides in problem.
3024.cs
C#
// Approach: Sort sides; check triangle validity then classify equilateral/isosceles/scalene.
// Time: O(1) Space: O(1)

public class Solution
{
    public string TriangleType(int[] nums)
    {
        Array.Sort(nums);
        if (nums[0] + nums[1] <= nums[2])
            return "none";
        if (nums[0] == nums[1] && nums[1] == nums[2])
            return "equilateral";
        if (nums[0] == nums[1] || nums[1] == nums[2])
            return "isosceles";
        return "scalene";
    }
}
Advertisement
Was this solution helpful?

Related Problems