Count Pairs with Smaller SumEasy

Count Pairs with Smaller Sum

EasyPractice library · +10 XPtwo pointerssortingcounting

Given a sorted array of N distinct integers and an integer K, find the number of pairs of indices (i, j) such that i < j and the sum of the elements at these indices is strictly less than K (i.e., arr[i] + arr[j] < K).

Input

The first line contains two integers N and K, separated by a space. The second line contains N space-separated integers representing the sorted array arr. Assume N >= 1.

Output

Print a single integer representing the count of such pairs.

Constraints

1 <= N <= 1000. The elements of the array are distinct integers. The values of array elements and K are between -10^9 and 10^9.

Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
5 10
1 3 5 7 9
Output
4

Pairs are (1,3), (1,5), (1,7), (1,9), (3,5), (3,7). Sums: 4, 6, 8, 10, 8, 10. Pairs with sum < 10: (1,3), (1,5), (1,7), (3,5). Count = 4.

Example 2

Input
3 5
-2 1 4
Output
2

Pairs are (-2,1), (-2,4), (1,4). Sums: -1, 2, 5. Pairs with sum < 5: (-2,1), (-2,4). Count = 2.

Hints