#C7891. Count Distinct Pairs with Given Sum
Count Distinct Pairs with Given Sum
Count Distinct Pairs with Given Sum
You are given an array of n integers and an integer K. Your task is to count the number of distinct pairs of elements in the array that add up to K. Two pairs are considered the same if they consist of the same two numbers, regardless of their order.
Formally, given an array A and an integer K, you need to find the number of distinct pairs \((a, b)\) such that:
[ a + b = K ]
and the pair \((a, b)\) is considered identical to \((b, a)\). Note that each distinct number pair is counted only once, even if it appears multiple times in the array.
inputFormat
The input is read from standard input (stdin) and has the following format:
T n1 K1 A1[0] A1[1] ... A1[n1-1] ... nT KT AT[0] AT[1] ... AT[nT-1]
Here, T
is the number of test cases. For each test case, the first line contains two integers n
(the number of elements in the array) and K
(the target sum). The next line contains n
space-separated integers representing the array elements.
outputFormat
For each test case, output a single line containing one integer: the number of distinct pairs in the array that sum to K. The output should be written to standard output (stdout).
## sample2
4 5
1 2 3 4
5 7
1 2 3 4 5
2
2
</p>