om-ganesh / codekata

1 stars 0 forks source link

Find closest pair to the target from two arrays #59

Open om-ganesh opened 4 years ago

om-ganesh commented 4 years ago

Given two sorted arrays and a number x, find the pair whose sum is closest to x and the pair has an element from each array. We are given two arrays ar1[0…m-1] and ar2[0..n-1] and a number x, we need to find the pair ar1[i] + ar2[j] such that absolute value of (ar1[i] + ar2[j] – x) is minimum. Example:

Input: ar1[] = {1, 4, 5, 7}; ar2[] = {10, 20, 30, 40}; x = 32
Output: 1 and 30

Input: ar1[] = {1, 4, 5, 7}; ar2[] = {10, 20, 30, 40}; x = 50
Output: 7 and 40

Ref: https://www.geeksforgeeks.org/given-two-sorted-arrays-number-x-find-pair-whose-sum-closest-x/

om-ganesh commented 4 years ago

image