Valid Triangle Number
Given an integer array nums, return the number of index triplets (i, j, k) with i < j < k whose values can be the side lengths of a triangle with positive area.
Three lengths a, b, and c form a triangle exactly when every pair sums to more than the third value. After sorting a triple so that a <= b <= c, only a + b > c needs to be checked, because the other two inequalities follow from a <= b <= c.
Examples
Input: [8,10,6,7,6,26,30,3,9]
Output: 36
Input: [30,17,19,7,4,17]
Output: 10
Hints
Sort the array first. For any sorted triple `(i, j, k)` with `i < j < k`, the only inequality you need to verify is `nums[i] + nums[j] > nums[k]`, because the other two are automatically satisfied.
Use the two-pointer technique: fix `k` as the largest side, then move `i` from the left and `j` from `k - 1`. When `nums[i] + nums[j] > nums[k]`, every index between `i` and `j - 1` also works with `j`.
The count of valid triangles for a fixed `k` and `j` is `j - i`. Accumulate this across all pairs. This runs in O(n^2), which is fast enough for the 1000-element limit.
Valid Triangle Number
Given an integer array `nums`, return the number of index triplets `(i, j, k)` with `i < j < k` whose values can be the side lengths of a triangle with positive area.