#C14153. Unique Pairs with Target Sum
Unique Pairs with Target Sum
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 in the list that sum up to T. Each pair should appear only once in the output even if there are duplicates in the input.
Details:
- You should use an efficient two-pointer approach after sorting the list. This approach finds pairs in O(n log n) time due to the sorting step.
- Be careful to skip duplicate numbers to ensure that each pair is unique.
- If no such pair exists, output nothing.
The mathematical condition for a valid pair \((a,b)\) is:
[ a+b=T ]
Good luck!
inputFormat
The first line contains an integer n representing the number of elements in the array.
The second line contains n space-separated integers which represent the elements of the array.
The third line contains an integer T which is the target sum.
outputFormat
For each unique pair that sums to T, output a single line containing the two numbers separated by a space. The order of pairs should follow the order determined by the two-pointer algorithm after sorting the array.
If there are no pairs that sum to T, output nothing.
## sample6
1 2 3 4 5 6
7
1 6
2 5
3 4
</p>