#C3806. Count Pairs with a Given Sum

    ID: 47274 Type: Default 1000ms 256MiB

Count Pairs with a Given Sum

Count Pairs with a Given Sum

You are given an array \(A\) of \(n\) integers 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\). In each valid pair, each element from the array can only be used once.

Note: A pair is considered distinct if the indices are different. The condition can be formally written in \(\LaTeX\) as:

[ \text{Find the number of pairs } (i,j) \text{ such that } i < j \text{ and } A_i + A_j = k. ]

This problem requires you to handle multiple test cases in one input.

inputFormat

The input is read from stdin and has the following format:

  1. The first line contains a single integer \(T\) representing the number of test cases.
  2. Each test case consists of two lines:
    • The first line of a test case contains two integers \(n\) and \(k\), where \(n\) is the number of elements in the array and \(k\) is the target sum.
    • The second line contains \(n\) space-separated integers representing the array \(A\).

Example:

3
5 7
1 2 3 4 5
4 1
1 5 1 5
3 8
3 5 2

outputFormat

For each test case, output a single integer on a new line representing the number of distinct pairs \((i, j)\) that satisfy \(A_i + A_j = k\). The output is written to stdout.

Example output for the sample input:

2
0
1
## sample
3
5 7
1 2 3 4 5
4 1
1 5 1 5
3 8
3 5 2
2

0 1

</p>