#K41537. Find Unique Pairs Summing to Target
Find Unique Pairs Summing to Target
Find Unique Pairs Summing to Target
You are given an array of integers and a target integer \(T\). Your task is to find all unique pairs \((a, b)\) from the array such that:
\(a + b = T\)
Each pair must be output with the smaller element first, i.e. \(a \le b\), and the final list of pairs should be sorted in ascending order by the first element of each pair. The result should be formatted as a Python-style list of tuples. For example, if there are two pairs \((a, b)\) and \((c, d)\), the output should be:
[(a, b), (c, d)]
If no valid pairs exist, output an empty list []
.
Note: Input is provided via standard input and output must be printed to standard output.
inputFormat
The input consists of three lines:
- The first line contains an integer \(n\) indicating the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the elements of the array. In case \(n = 0\), the second line will be empty.
- The third line contains an integer \(T\) which is the target sum.
outputFormat
Output a single line containing the list of unique pairs in Python list-of-tuples format. Each pair should be printed as \((a, b)\) (with no extra spaces inside the parentheses other than after the comma) and the overall list should follow exactly the format in the examples. If there are no pairs, output []
.
7
2 4 3 5 7 8 9
10
[(2, 8), (3, 7)]