#C4808. Count Pairs with Sum
Count Pairs with Sum
Count Pairs with Sum
You are given an integer array A
of size N and an integer k. Your task is to count the number of distinct pairs (i, j) with i < j such that:
$A_i + A_j = k$
For example, consider the array [1, 5, 7, -1]
with k = 6. The pairs that satisfy the condition are (1, 5)
and (7, -1)
, so the answer is 2
.
When there are duplicate numbers in the array, every distinct occurrence is considered a separate element. For instance, in the array [1, 1, 1, 1, 1]
with k = 2 there are 10 valid pairs, since any two of the five 1
s can form a valid pair.
Note: The solution should process multiple test cases in one execution.
inputFormat
The first line of input contains a single integer T denoting the number of test cases.
Each test case consists of two lines:
The first line contains two integers N and k, where N is the number of elements of the array and k is the target sum.
The second line contains N space-separated integers representing the array A
. If N is 0
, the second line will be empty.
outputFormat
For each test case, output a single integer on a new line representing the number of distinct pairs (i, j) (with i < j) from the array such that Ai + Aj = k.
## sample2
4 6
1 5 7 -1
5 2
1 1 1 1 1
2
10
</p>