#C14768. Unique Pair Finder
Unique Pair Finder
Unique Pair Finder
You are given a list of numbers and a target sum \(T\). Your task is to find all unique pairs \((a, b)\) from the list such that they satisfy the equation \(a + b = T\). The pairs must be returned in ascending order based on the first element. Each pair \((a, b)\) should be represented with the smaller number first (i.e. \(a \le b\)).
For example, if the list is [2, -1, 3, 6, 4, -3, 0, 5, -5]
and \(T = 5\), the correct output is [(-1, 6), (0, 5), (2, 3)]
. Note that even if a valid pair can be found in multiple ways (for example, when duplicate numbers exist), it should be reported only once.
Input/Output Format:
The input will be read from stdin
and the output should be printed to stdout
in the format described below.
Good luck!
inputFormat
The first line of input contains two values: an integer \(N\) (the number of elements) and a target sum \(T\) (which can be an integer or float). The second line contains \(N\) space-separated numbers (each of which may be an integer or a float).
outputFormat
Print a single line representing the list of unique pairs in the following format:
[ (a1, b1), (a2, b2), ..., (ak, bk) ]
The pairs must be sorted in ascending order by the first element. If no such pairs exist, print []
.
9 5
2 -1 3 6 4 -3 0 5 -5
[(-1, 6), (0, 5), (2, 3)]