Find and return a pair of integers in a sorted array (all integers are positive) that, when summed up, bring you the closest to the value of k.
Example Input: [5, 8, 14, 17, 25, 40, 43] k: 35
Expected Output: (8, 25) since 8 + 25 = 33 which is closest to 35 (sum could be equals to, smaller or larger than k)
Complete the function findPairClosestToK in the editor.
findPairClosestToK has the following parameters:
- 1.
int[] arr: a sorted array of integers - 2.
int k: the target sum
Returns
int[]: an array of two integers that are closest to the target sum k
The other question was ~~
arr = [5, 8, 14, 17, 25, 40, 43] k = 35 return = [8, 25]
The pair of integers (8, 25) when summed up, give us 33 which is the closest to the value of k which is 35. The sum could be equal to, smaller or larger than k, but in this case, 33 is the closest sum to k that can be obtained from the given array.
- Merge Three Sorted ArraysPHONE SCREEN · Seen May 2026
- Diagonal Traverse (for E4 ;)PHONE SCREEN · Seen Mar 2025
- Find Peak ElementPHONE SCREEN · Seen Mar 2025
- Get Minimum Round Trip Cost (: for E4 && E5 :)PHONE SCREEN · Seen Feb 2025
- Max Consecutive Ones III (for E5 :)PHONE SCREEN · Seen Feb 2025
- Nested List Weight Sum (for E4)PHONE SCREEN · Seen Feb 2025
- Sliding Window Average (Meta Canada, E4/E5 :)PHONE SCREEN · Seen Dec 2024
- Build Blocks and Obstacles on a Number LineSeen Oct 2024
public int[] findPairClosestToK(int[] arr, int k) {
// write your code here
}