#C12215. Find Pairs With Target Sum
Find Pairs With Target Sum
Find Pairs With Target Sum
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 formatted in ascending order, i.e. \(a \le b\), and each unique pair should appear only once in the output. The final list of pairs should be sorted in lexicographical order (first by \(a\), then by \(b\)).
If no such pair exists, output an empty list []
.
Input Format: The first line contains an integer \(n\), representing the number of elements in the array. The second line contains \(n\) space-separated integers. The third line contains the target integer \(T\).
Output Format: Print the list of unique pairs in the following Python-style format:
[(a, b), (c, d), ...]
inputFormat
The input is read from standard input (stdin) and consists of three lines:
- The first line contains an integer \(n\) — the number of integers in the array.
- The second line contains \(n\) space-separated integers.
- The third line contains an integer \(T\) — the target sum.
outputFormat
Output to standard output (stdout) a Python-style list of unique pairs that sum to \(T\), where each pair is in the format \((a, b)\). If there are no such pairs, output an empty list []
.
6
1 2 3 4 5 6
7
[(1, 6), (2, 5), (3, 4)]