LeetCodee

611. Valid Triangle Number

Problem Description

Given an integer array nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.

Example 1:

Input: nums = [2,2,3,4]
Output: 3
Explanation: Valid combinations are: 
2,3,4 (using the first 2)
2,3,4 (using the second 2)
2,2,3

Example 2:

Input: nums = [4,2,3,4]
Output: 4

Constraints:

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 1000

Solution

Python Solution

class Solution:
    def triangleNumber(self, nums: List[int]) -> int:
        nums.sort()
        count = 0
        n = len(nums)
        
        for i in range(n-1, 1, -1):
            left = 0
            right = i - 1
            
            while left < right:
                if nums[left] + nums[right] > nums[i]:
                    count += right - left
                    right -= 1
                else:
                    left += 1
                    
        return count

Time Complexity: O(n²)

Where n is the length of the input array.

Space Complexity: O(1)

Only using constant extra space.

Java Solution

class Solution {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int count = 0;
        int n = nums.length;
        
        for (int i = n-1; i > 1; i--) {
            int left = 0;
            int right = i - 1;
            
            while (left < right) {
                if (nums[left] + nums[right] > nums[i]) {
                    count += right - left;
                    right--;
                } else {
                    left++;
                }
            }
        }
        
        return count;
    }
}

Time Complexity: O(n²)

Where n is the length of the input array.

Space Complexity: O(1)

Only using constant extra space.

C++ Solution

class Solution {
public:
    int triangleNumber(vector& nums) {
        sort(nums.begin(), nums.end());
        int count = 0;
        int n = nums.size();
        
        for (int i = n-1; i > 1; i--) {
            int left = 0;
            int right = i - 1;
            
            while (left < right) {
                if (nums[left] + nums[right] > nums[i]) {
                    count += right - left;
                    right--;
                } else {
                    left++;
                }
            }
        }
        
        return count;
    }
};

Time Complexity: O(n²)

Where n is the length of the input array.

Space Complexity: O(1)

Only using constant extra space.

JavaScript Solution

/**
 * @param {number[]} nums
 * @return {number}
 */
var triangleNumber = function(nums) {
    nums.sort((a, b) => a - b);
    let count = 0;
    const n = nums.length;
    
    for (let i = n-1; i > 1; i--) {
        let left = 0;
        let right = i - 1;
        
        while (left < right) {
            if (nums[left] + nums[right] > nums[i]) {
                count += right - left;
                right--;
            } else {
                left++;
            }
        }
    }
    
    return count;
};

Time Complexity: O(n²)

Where n is the length of the input array.

Space Complexity: O(1)

Only using constant extra space.

C# Solution

public class Solution {
    public int TriangleNumber(int[] nums) {
        Array.Sort(nums);
        int count = 0;
        int n = nums.Length;
        
        for (int i = n-1; i > 1; i--) {
            int left = 0;
            int right = i - 1;
            
            while (left < right) {
                if (nums[left] + nums[right] > nums[i]) {
                    count += right - left;
                    right--;
                } else {
                    left++;
                }
            }
        }
        
        return count;
    }
}

Time Complexity: O(n²)

Where n is the length of the input array.

Space Complexity: O(1)

Only using constant extra space.

Approach Explanation

The solution uses sorting and two-pointer technique:

  1. Key Insights:
    • Triangle inequality
    • Sorting optimization
    • Two-pointer technique
    • Count accumulation
  2. Algorithm Steps:
    • Sort array
    • Fix largest side
    • Use two pointers
    • Count valid triangles

Implementation Details:

  • Array sorting
  • Two pointers
  • Count tracking
  • Inequality check

Optimization Insights:

  • Sorted array
  • Skip duplicates
  • Early termination
  • Efficient counting

Edge Cases:

  • Empty array
  • Small array
  • Zero values
  • Duplicate values