#C3137. Find Pairs with a Given Sum
Find Pairs with a Given Sum
Find Pairs with a Given Sum
You are given a list of integers and a target sum. Your task is to find all unique pairs of integers from the list such that the sum of the two numbers is equal to the given target. A pair (a, b) is considered unique if there is no other pair (a, b) or (b, a) in the answer. Each pair should be output in increasing order (i.e. the smaller number first), and the overall list of pairs should be sorted in ascending order by the first element (and then by the second element if necessary).
Note: If no such pair exists, output None
.
The algorithmic challenge involves using an efficient approach to track the numbers you have seen and determine whether a valid complement exists. In mathematical notation, for every number \( x \) in the list, you must check if there exists a number \( y \) (with \( y = target - x \)) that has already been processed. The relationship can be written as:
[ x + y = target ]
</p>inputFormat
The input consists of two lines:
- The first line contains a space-separated list of integers.
- The second line contains a single integer representing the target sum.
Input is read from stdin.
outputFormat
If there is at least one valid pair, output each pair in a separate line with the two integers separated by a space. The pairs must be sorted in ascending order. If no valid pair exists, output a single line with the text None
. Output is written to stdout.
1 3 2 2 4 5
6
1 5
2 4
</p>