#K7091. Find Unique Pairs with Given Sum

    ID: 33413 Type: Default 1000ms 256MiB

Find Unique Pairs with Given Sum

Find Unique Pairs with Given Sum

You are given a list of integers and an integer target value \(T\). Your task is to find all unique pairs of integers from the list that sum up to \(T\). Each pair should be output in increasing order (i.e. the smaller number first), and the overall list of pairs should be sorted lexicographically.

Example:

Input:
8
1 2 3 4 5 -1 -2 -3
4

Output: -1 5 1 3

</p>

If no such pairs exist, the program should not output anything.

Note: Each number from the input list can be used at most once in forming a pair (i.e. the pair \(x,y\) is formed only when \(x\) and \(y\) appear with appropriate frequency, and pairs with the same numbers in different order are considered duplicates). Also, pairs with identical numbers (e.g. \(3,3\)) are only valid if there are at least two occurrences of that number.

inputFormat

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

  1. An integer \(N\) representing the number of elements in the list.
  2. A line with \(N\) space-separated integers.
  3. An integer \(T\) which is the target sum.

For example:

8
1 2 3 4 5 -1 -2 -3
4

outputFormat

The output is written to standard output (stdout). For each valid pair, output two integers separated by a space on a separate line. The pairs must be listed in lexicographical order (sorted by the first number then the second). If there is no valid pair, do not print anything.

For example, the expected output for the sample input above is:

-1 5
1 3
## sample
8
1 2 3 4 5 -1 -2 -3
4
-1 5

1 3

</p>