Valid Triangle Number

Given an array of integers, count how many triplets can form the sides of a valid triangle. Sorting lets you fix the largest side and count valid pairs with two pointers.

Input Format

nums = array of non-negative integers

Output Format

count of valid triangles

Constraints

  • 3 <= nums.length <= 1000; 0 <= nums[i] <= 10^3

Examples

Example 1:

Input:

nums = [2,2,3,4]

Output:

3

Explanation:

The valid triangles are (2,3,4), (2,3,4), and (2,2,3).

Example 2:

Input:

nums = [4,2,3,4]

Output:

4

Explanation:

There are four valid triplets after sorting.

Loading...
Valid Triangle Number