X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Sum Pair closest to X
Easy Accuracy: 52.75% Submissions: 1910 Points: 2

Given a sorted array arr[] of size N and a number X, find a pair in array whose sum is closest to X.

Example 1:

Input:
N = 6, X = 54
arr[] = {10, 22, 28, 29, 30, 40}
Output: 22 30
Explanation: As 22 + 30 = 52 is closest to 54.


Example 2:

Input:
N = 5, X = 10
arr[] = {1, 2, 3, 4, 5}
Output: 4 5
Explanation: As 4 + 5 = 9 is closest to 10.


Your Task:
You don't need to read input or print anything. Your task is to complete the function sumClosest() which takes arr[] of size n and as input parameters and returns a list containing 2 integers denoting the required pair. If multiple pairs are possible return the largest sum pair.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)


Constraints:
2 <= N <= 105
0 <= X <= 106
0 <= arr[i] <= 106

 

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Sum Pair closest to X

Output Window