#K2941. Count Unique Pairs

    ID: 24848 Type: Default 1000ms 256MiB

Count Unique Pairs

Count Unique Pairs

Given an array of integers and a target value \(T\), determine the number of distinct pairs \((a, b)\) such that \(a + b = T\). Each element in the array may be used in at most one pair. Two pairs are considered distinct if they involve different indices or values. The order of elements in the pair does not matter.

Example: For the array [1, 1, 2, 45, 46, 46] and \(T = 47\), the valid pairs are \((1, 46)\) (using one of the 1s and one of the 46s) and \((2, 45)\), so the answer is 2.

You are encouraged to solve this problem efficiently using a two-pointer strategy after sorting the array.

inputFormat

The input is provided via standard input (stdin) and consists of three lines:

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

outputFormat

Output a single integer to standard output (stdout): the number of distinct pairs in the array whose sum is equal to \(T\).

## sample
6
1 1 2 45 46 46
47
2