FastPrepFastPrep
Problem Brief

Find Pair Closest to K (for E5 :)

FULLTIMEPHONE SCREEN

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)

Function Description

Complete the function findPairClosestToK in the editor.

findPairClosestToK has the following parameters:

  1. 1. int[] arr: a sorted array of integers
  2. 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 ~~

1Example 1

Input
arr = [5, 8, 14, 17, 25, 40, 43], k = 35
Output
[8, 25]
Explanation

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.

public int[] findPairClosestToK(int[] arr, int k) {
  // write your code here
}
Input

arr

[5, 8, 14, 17, 25, 40, 43]

k

35

Output

[8, 25]

Sign in to submit your solution.