Input: arr[] = [4, 6, 3, 7]
Output: 3
Explanation: There are three triangles possible [3, 4, 6], [4, 6, 7] and [3, 6, 7].
Note that [3, 4, 7] is not a possible triangle.
Input: arr[] = [10, 21, 22, 100, 101, 200, 300]
Output: 6
Explanation: There can be 6 possible triangles:
[10, 21, 22], [21, 100, 101], [22, 100, 101], [10, 100, 101], [100, 101, 200] and [101, 200, 300]
Input: arr[] = [1, 2, 3]
Output: 0
Examples: No triangles are possible.
The idea is to sort the array in ascending order. Then, use two nested loops: the outer loop to fix the first side, and the inner loop to fix the second side.
Next, we find the farthest index for the third side (beyond the indices of the first two sides), such that its value is less than the sum of the first two sides, using Binary Search. So a range of values for third side can be found, where it is guaranteed that its length is greater than the other individual sides but less than the sum of both sides. Add this range size to the result.
The idea is to sort the array to simplify checking the triangle inequality. Then, for each element (treated as the largest side), use two pointers technique to find count of pairs of smaller sides that can form a triangle with it.
For this, the two pointers are initialized as: one pointer (left) starts at index 0, and the other pointer (right) is positioned just before the current largest side (arr[i]).
Now, compare the sum of arr[left] + arr[right] with the current largest side (arr[i]):
- If the sum is greater than or equal to arr[i], a valid triangle can be formed. Count all valid pairs between left and right, then move the right pointer to the left to explore smaller side values.
- If the sum is less than arr[i], increment the left pointer to increase the sum and check larger values.