Find the closest pair from two sorted arrays - GeeksforGeeks
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:
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 30O(N)
The idea is to start from left side of one array and right side of another array, and use the algorithm same as step 2 of above approach. Following is detailed algorithm.
1) Initialize a variable diff as infinite (Diff is used to store the difference between pair and x). We need to find the minimum diff. 2) Initialize two index variables l and r in the given sorted array. (a) Initialize first to the leftmost index in ar1: l = 0 (b) Initialize second the rightmost index in ar2: r = n-1 3) Loop while l < m and r >= 0 (a) If abs(ar1[l] + ar2[r] - sum) < diff then update diff and result (b) Else if(ar1[l] + ar2[r] < sum ) then l++ (c) Else r-- 4) Print the result.A little optimization: when diff is 0, exit the loop.
void
printClosest(
int
ar1[],
int
ar2[],
int
m,
int
n,
int
x)
{
// Initialize the diff between pair sum and x.
int
diff = INT_MAX;
// res_l and res_r are result indexes from ar1[] and ar2[]
// respectively
int
res_l, res_r;
// Start from left side of ar1[] and right side of ar2[]
int
l = 0, r = n-1;
while
(l<m && r>=0)
{
// If this pair is closer to x than the previously
// found closest, then update res_l, res_r and diff
if
(
abs
(ar1[l] + ar2[r] - x) < diff)
{
res_l = l;
res_r = r;
diff =
abs
(ar1[l] + ar2[r] - x);
}
// If sum of this pair is more than x, move to smaller
// side
if
(ar1[l] + ar2[r] > x)
r--;
else
// move to the greater side
l++;
}
// Print the result
cout <<
"The closest pair is ["
<< ar1[res_l] <<
", "
<< ar2[res_r] <<
"] \n"
;
}
We can do it in O(n) time using following steps.
1) Merge given two arrays into an auxiliary array of size m+n using merge process of merge sort. While merging keep another boolean array of size m+n to indicate whether the current element in merged array is from ar1[] or ar2[].
1) Merge given two arrays into an auxiliary array of size m+n using merge process of merge sort. While merging keep another boolean array of size m+n to indicate whether the current element in merged array is from ar1[] or ar2[].
2) Consider the merged array and use the linear time algorithm to find the pair with sum closest to x. One extra thing we need to consider only those pairs which have one element from ar1[] and other from ar2[], we use the boolean array for this purpose.
Read full article from Find the closest pair from two sorted arrays - GeeksforGeeks