/** we need to find 3 number, i < j < k, and a[i] + a[j] > a[k];

* if we sort the array, then we can easily use two pointer to find all the pairs we need.

* if at some point a[left] + a[right] > a[i], all the elements from left to right-1 are valid.

* because they are all greater then a[left];

* so we do count += right - left; and right--

*

* otherwise, we increment left till we get a valid pair.

```
public int triangleNumber(int[] nums) {
if (nums == null || nums.length <= 2) {
return 0;
}
Arrays.sort(nums);
int count = 0;
for (int i = 0; i < nums.length; i++) {
int left = 0, right = i-1;
while (left < right) {
if (nums[left] + nums[right] > nums[i]) {
count += right - left;
right--;
} else {
left++;
}
}
}
return count;
}
```