#C10467. Count Pairs with Given Sum
Count Pairs with Given Sum
Count Pairs with Given Sum
You are given an array of integers and a target value. Your task is to determine the number of distinct pairs (i, j) such that a[i] + a[j] = target. A pair is counted only once even if the same number appears multiple times. For example, given the array [1, 5, 7, -1]
and target 6
, the valid pairs are (1,5)
and (7,-1)
, so the answer is 2.
Note: A pair (i, j) and (j, i) are considered the same, so count them only once.
The problem may involve multiple test cases.
inputFormat
The first line of input contains an integer T
representing the number of test cases.
Each test case consists of the following two lines:
- The first line contains two space-separated integers:
n
(the number of elements in the array) andtarget
(the target sum). - The second line contains
n
space-separated integers representing the array elements.
outputFormat
For each test case, output a single integer on a new line representing the number of pairs whose sum equals the target.
## sample4
4 6
1 5 7 -1
4 10
1 5 7 -1
4 2
1 1 1 1
4 1
1 -1 0 2
2
0
6
2
</p>