Easyhashingarrays
Pair Sum Count
A warehouse wants to pack items two at a time so that each box weighs exactly T. Given the weights of n items, count the pairs of positions (i, j) with i < j whose weights add up to exactly T.
Input
The first line has n and T. The second line has n integers, the weights.
Output
Print the number of pairs.
Constraints
- 1 ≤ n ≤ 2 × 10^4
- −10^9 ≤ weight, T ≤ 2 × 10^9
- The answer fits in a 64-bit integer
Sample 1
Input
5 6 1 5 3 3 4
Output
2
The pairs are (1, 5) at positions 1 and 2, and (3, 3) at positions 3 and 4.
Sample 2
Input
4 2 1 1 1 1
Output
6
Any two of the four 1s make 2, giving 4 × 3 / 2 = 6 pairs.
Time limit 3s (4.5s for Python in the browser). Your code runs on your device, so timings vary a little between machines.