#K41527. Find Unique Pairs with Target Sum
Find Unique Pairs with Target Sum
Find Unique Pairs with Target Sum
You are given a list of n integers and a target integer t. Your task is to find all unique pairs of numbers from the list that add up to the target sum t. 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 based on the first element of each pair.
Note: Each pair is considered unique. That is, even if there are duplicate numbers in the list, a pair (a, b) should only appear once. If no such pairs exist, output []
.
Input Format: The input is given via standard input (stdin) as follows:
- The first line contains two space-separated integers:
n
(the number of integers) andt
(the target sum). - The second line contains
n
space-separated integers.
Output Format: If at least one pair is found, print each unique pair on a separate line with the two numbers separated by a space. The pairs must be sorted in ascending order, first by the first number then by the second. If no valid pair exists, output []
.
In mathematical terms, you are to find all pairs \((a, b)\) such that:
\[ a + b = t, \quad a \leq b \]inputFormat
The first line contains two integers n
(number of elements) and t
(target sum). The second line contains n
integers separated by spaces.
outputFormat
If there exists at least one pair of integers that sum to t
, output each pair on a separate line in the format "a b", where a ≤ b
. The pairs should be sorted in ascending order based on the first element, and then the second element. If no valid pair exists, output []
(without quotes).
3 7
1 2 3
[]