118. Closest Pair Sum
Challenge1000 ms256 MBSolved by 0%
You are given an array and a target k.
Print the sum of the two elements whose total is closest to k. The two elements must be
at different positions.
If two different sums are equally close, print the smaller one.
Constraints - `2 ≤ n ≤ 200000` - `-1000000000 ≤ a[i], k ≤ 1000000000`
Input
The first line contains two integers n and the target.
The second line contains n space-separated integers.
Output
Print one integer, the sum of two elements at different positions that is closest to the target. On a tie print the SMALLER sum.
Input5 10
1 3 4 7 10
Output10
Notehas a pair hitting the target exactly
Input4 8
1 2 3 4
Output7
Notehas no exact hit, so the closest sum below wins
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.