#C1131. Count Distinct Pairs with Given Sum

    ID: 40612 Type: Default 1000ms 256MiB

Count Distinct Pairs with Given Sum

Count Distinct Pairs with Given Sum

Given an integer array \(A\) and an integer target \(T\), count the number of distinct pairs \((i, j)\) such that \(i < j\) and \(A[i] + A[j] = T\).

Note: For any pair formed by two identical numbers \(x\) (i.e. when \(x = T-x\)), the number of pairs is given by the binomial coefficient \( \binom{n}{2} = \frac{n \times (n-1)}{2} \ \).

The input is given via standard input where the first line contains an integer \(n\) (the number of elements in the array), followed by a line with \(n\) integers (the array elements), and finally a line with the target value \(T\). The output is a single integer representing the count of distinct pairs.

Example:

Input:
5
1 5 7 -1 5
6

Output: 3

</p>

inputFormat

The input is read from standard input (stdin). The format is as follows:

  • The first line contains an integer \(n\), the number of elements in the array.
  • The second line contains \(n\) space-separated integers representing the array.
  • The third line contains an integer \(T\), the target sum.

outputFormat

The output is printed to standard output (stdout) as a single integer denoting the count of distinct pairs \((i, j)\) such that \(i < j\) and \(A[i] + A[j] = T\).

## sample
5
1 5 7 -1 5
6
3