Problems > Closest Pair Sum

Closest Pair Sum

Difficulty: 1500 3500 ms 128 MB

sorting two-pointers

Choose two different elements whose sum is as close as possible to the target T. Print that sum; if two sums are equally close, print the smaller sum.

Input: the first line has N and T; the second line has N integers.

Output: the achievable pair sum closest to T (smaller one on ties).

Constraints:

  • 2 ≤ N ≤ 105
  • |ai|, |T| ≤ 2×109

Sample Tests

Input 1
5 10
1 3 5 8 12
Output 1
9
Input 2
2 0
-3 9
Output 2
6

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.