#K42782. Find All Pairs with a Given Sum
Find All Pairs with a Given Sum
Find All Pairs with a Given Sum
You are given an array of unique integers and an integer target value \(T\). Your task is to find all pairs of integers \((a, b)\) from the array such that:
[ a+b=T ]
Each valid pair should satisfy \(a+b=T\) and be output in sorted order (i.e. the pair \((a, b)\) with \(a \leq b\)). The resulting list of pairs must be sorted in ascending order by the first element and then by the second element. If no such pair exists, output None
.
Note: The input and output are handled via standard input (stdin) and standard output (stdout) respectively.
inputFormat
The input 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 (all unique).
- The third line contains an integer \(T\), the target sum.
outputFormat
If there is at least one pair \((a, b)\) whose sum is equal to \(T\), output each pair on a separate line in the format "a b" where \(a\) and \(b\) are the two numbers in the pair and \(a \leq b\). The pairs must be output in sorted order (first by \(a\) then by \(b\)). If no pairs exist, output a single line with the text None
.
7
2 4 3 5 7 8 9
10
2 8
3 7
</p>