#C9947. Unique Pair Sum Finder
Unique Pair Sum Finder
Unique Pair Sum Finder
You are given an array of integers and a target sum. Your task is to find all unique pairs of numbers in the array that add up to the target sum. Each pair must be printed in non-descending order, and the overall list of pairs must also be sorted in lexicographical (non-descending) order.
The answer should be produced by reading the input from stdin and writing the output to stdout. If no valid pair exists, output an empty list ([]
).
Note: A pair is considered unique if it is not a duplicate of any other pair, even if the numbers appear multiple times in the array.
The problem can be formalized as follows:
Let \(A = [a_1, a_2, \dots, a_n]\) be the input array and let \(T\) be the target sum. Find all pairs \((a_i, a_j)\) with \(i < j\) such that \(a_i + a_j = T\). Each pair \((a, b)\) should satisfy \(a \leq b\) and the final list of pairs should be sorted lexicographically.
inputFormat
The first line contains an integer \(n\), representing 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
Output a list of pairs in the format [[a, b], [c, d], ...]
where each pair sums up to the target value \(T\), and both the numbers in each pair and the list itself are in non-descending order. If no such pairs exist, output an empty list []
.
11
1 2 3 4 5 6 7 8 9 1 3
10
[[1, 9], [2, 8], [3, 7], [4, 6]]