Problem 307
Count Pairs with Given Sum
Easy · 10 pts · Array, Hash Table
Problem
Count the pairs of positions (i, j) with i < j such that A[i] + A[j] equals the target K. Pairs are counted by position, not by value: if the same value appears several times, every combination of positions counts as a different pair. A position cannot be paired with itself.
Input
The first line contains N and K. The second line contains N integers.
Output
Print the number of pairs.
Constraints
1 <= N <= 100000; -1000000000 <= A[i], K <= 1000000000 (the answer fits in a 64-bit integer)
Example input
5 6 1 5 7 -1 5
Example output
3
Explanation
With K = 6 in 1 5 7 -1 5, the pairs are (1, 5) using the first 5, (1, 5) using the second 5, and (7, -1), so there are 3 pairs.