#C6535. Closest Pair Sum Problem
Closest Pair Sum Problem
Closest Pair Sum Problem
You are given an array of integers and an integer k. Your task is to find a pair of distinct elements from the array such that the absolute difference between their sum and k is minimized. In other words, if the two chosen numbers are a and b, you need to minimize \( |a+b-k| \).
If there are multiple pairs having the same minimum difference, choose the pair with the smallest first element. If there is still a tie, choose the pair with the smallest second element.
Input details: Each test case is given as follows: the first number is the number of elements in the array, followed by the target k value, and then the list of integers.
Constraints: Use a two-pointer strategy on the sorted array to efficiently determine the pair with the closest sum to \( k \). This strategy works in \( O(n \log n) \) due to sorting.
inputFormat
The first line contains an integer T representing the number of test cases. Then for each test case:
- The first line of each test case contains two integers: n (the number of elements in the array) and k (the target sum).
- The second line contains n space-separated integers representing the array.
Example:
3 5 10 1 2 3 4 5 4 1 -1 2 1 -4 4 8 5 1 3 5
outputFormat
For each test case, output a single line with two space-separated integers representing the pair that has a sum closest to k under the tie-breaking rules.
Example:
4 5 -1 2 3 5## sample
1
5 10
1 2 3 4 5
4 5
</p>