#C13436. Unique Pairs with Given Sum
Unique Pairs with Given Sum
Unique Pairs with Given Sum
The goal is to find all unique pairs in an array that sum to a given target value. Each pair should be output with the smaller element first, and the list of pairs should be sorted in lexicographical order.
Formally, given an array of integers \(A\) and a target sum \(S\), find all pairs \((a, b)\) such that \(a \leq b\) and \(a+b=S\), without repeating any pair even if numbers appear multiple times in the array.
The output must follow a Python list representation format. For example, if the pairs are \((-1, 7)\) and \((1, 5)\), the output should be: [(-1, 7), (1, 5)]
.
inputFormat
The input is read from standard input (stdin) and consists of three lines:
- The first line contains an integer \(n\), 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 an integer representing the target sum \(S\).
outputFormat
The output should be printed to standard output (stdout) as a single line containing a Python-like list of tuples. Each tuple represents a unique pair \((a, b)\) with \(a \leq b\) that sums to the target \(S\). The list of pairs should be sorted in ascending order.
## sample5
1 5 7 -1 5
6
[(-1, 7), (1, 5)]