Count Pairs with Specific SumEasy

Count Pairs with Specific Sum

EasyPractice library · +10 XPsliding windowtwo pointersarrays

Given a sorted array of N distinct integers and an integer K, find the number of pairs (i, j) such that i < j and arr[i] + arr[j] = K. The array is guaranteed to be sorted in non-decreasing order. You should use a sliding window (or two pointers) approach for efficiency.

Input

The first line contains two integers N and K, where N is the size of the array and K is the target sum. The second line contains N space-separated integers representing the sorted array arr.

Output

Print a single integer, the count of pairs (i, j) such that i < j and arr[i] + arr[j] = K.

Constraints

1 <= N <= 1000; 1 <= arr[i] <= 10^9; 1 <= K <= 2 * 10^9; The array arr is sorted in non-decreasing order and contains distinct integers.

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

Example 1

Input
5 10
1 3 5 7 9
Output
2

Pairs are (1, 9) and (3, 7).

Example 2

Input
4 7
1 2 3 4
Output
1

Pair is (3, 4).

Hints