Description
Given an integer array nums, return the number of triplets that could form a valid triangle (sum of two sides > third).
Examples
Input:
nums = [2,2,3,4]Output:
3Explanation:
(2,3,4),(2,3,4),(2,2,3).
Input:
nums = [1,2,1,1]Output:
0Explanation:
No valid triangles can be formed. For any three sides to form a triangle, the sum of any two sides must be greater than the third side. The possible triplets are (1,2,1), (1,1,1), (2,1,1), and (1,2,1), but none satisfy the triangle inequality: 1+1=2 which is not greater than 2, and 1+1=2 which is not greater than 2.
Input:
nums = [5,3,4,4]Output:
3Explanation:
Valid triangles are (3,4,4), (5,4,4), and (3,4,5). For (3,4,4): 3+4>4, 4+4>3, 3+4>4 ✓. For (5,4,4): 5+4>4, 4+4>5, 5+4>4 ✓. For (3,4,5): 3+4>5, 4+5>3, 3+5>4 ✓. The triplet (5,3,4) is the same as (3,4,5).
Constraints
- •
1 ≤ nums.length ≤ 1000