#C2404. Find All Unique Pairs with Given Sum
Find All Unique Pairs with Given Sum
Find All Unique Pairs with Given Sum
You are given an array of integers and a target integer \(s\). Your task is to find all unique pairs \( (a, b)\) from the array such that \(a + b = s\). Each pair should be output in ascending order (i.e. \(a \le b\)) and the list of pairs should be sorted in lexicographical order.
Example: For the input array [1, 2, 3, 4, 5]
and \(s = 5\), the unique pairs that add up to 5 are [1, 4]
and [2, 3]
, so the output should be [[1, 4], [2, 3]]
.
Note: If there are duplicate numbers in the array, each unique pair should only appear once.
inputFormat
The input is read from stdin and has the following 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 an integer \(s\), the target sum.
outputFormat
Print the list of unique pairs in stdout in a Python list format. Each pair should be represented as a two-element list. If no such pair exists, print an empty list []
.
5
1 2 3 4 5
5
[[1, 4], [2, 3]]