# Find the closest pair from two sorted arrays

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

**We strongly recommend to minimize your browser and try this yourself first.**

A **Simple Solution** is to run two loops. The outer loop considers every element of first array and inner loop checks for the pair in second array. We keep track of minimum difference between ar1[i] + ar2[j] and x.

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[].

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.

**Can we do it in a single pass and O(1) extra space?**

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 = 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.

Following is the implementation of this approach.

## C++

`// C++ program to find the pair from two sorted arays such ` `// that the sum of pair is closest to a given number x ` `#include <iostream> ` `#include <climits> ` `#include <cstdlib> ` `using` `namespace` `std; ` ` ` `// ar1[0..m-1] and ar2[0..n-1] are two given sorted arrays ` `// and x is given number. This function prints the pair from ` `// both arrays such that the sum of the pair is closest to x. ` `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"` `; ` `} ` ` ` `// Driver program to test above functions ` `int` `main() ` `{ ` ` ` `int` `ar1[] = {1, 4, 5, 7}; ` ` ` `int` `ar2[] = {10, 20, 30, 40}; ` ` ` `int` `m = ` `sizeof` `(ar1)/` `sizeof` `(ar1[0]); ` ` ` `int` `n = ` `sizeof` `(ar2)/` `sizeof` `(ar2[0]); ` ` ` `int` `x = 38; ` ` ` `printClosest(ar1, ar2, m, n, x); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find closest pair in an array ` `class` `ClosestPair ` `{ ` ` ` `// ar1[0..m-1] and ar2[0..n-1] are two given sorted ` ` ` `// arrays/ and x is given number. This function prints ` ` ` `// the pair from both arrays such that the sum of the ` ` ` `// pair is closest to x. ` ` ` `void` `printClosest(` `int` `ar1[], ` `int` `ar2[], ` `int` `m, ` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` `// Initialize the diff between pair sum and x. ` ` ` `int` `diff = Integer.MAX_VALUE; ` ` ` ` ` `// res_l and res_r are result indexes from ar1[] and ar2[] ` ` ` `// respectively ` ` ` `int` `res_l = ` `0` `, res_r = ` `0` `; ` ` ` ` ` `// 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` `(Math.abs(ar1[l] + ar2[r] - x) < diff) ` ` ` `{ ` ` ` `res_l = l; ` ` ` `res_r = r; ` ` ` `diff = Math.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 ` ` ` `System.out.print(` `"The closest pair is ["` `+ ar1[res_l] + ` ` ` `", "` `+ ar2[res_r] + ` `"]"` `); ` ` ` `} ` ` ` ` ` `// Driver program to test above functions ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `ClosestPair ob = ` `new` `ClosestPair(); ` ` ` `int` `ar1[] = {` `1` `, ` `4` `, ` `5` `, ` `7` `}; ` ` ` `int` `ar2[] = {` `10` `, ` `20` `, ` `30` `, ` `40` `}; ` ` ` `int` `m = ar1.length; ` ` ` `int` `n = ar2.length; ` ` ` `int` `x = ` `38` `; ` ` ` `ob.printClosest(ar1, ar2, m, n, x); ` ` ` `} ` `} ` `/*This code is contributed by Rajat Mishra */` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the pair from ` `# two sorted arays such that the sum ` `# of pair is closest to a given number x ` `import` `sys ` ` ` `# ar1[0..m-1] and ar2[0..n-1] are two ` `# given sorted arrays and x is given ` `# number. This function prints the pair ` `# from both arrays such that the sum ` `# of the pair is closest to x. ` `def` `printClosest(ar1, ar2, m, n, x): ` ` ` ` ` `# Initialize the diff between ` ` ` `# pair sum and x. ` ` ` `diff` `=` `sys.maxsize ` ` ` ` ` `# res_l and res_r are result ` ` ` `# indexes from ar1[] and ar2[] ` ` ` `# respectively. Start from left ` ` ` `# side of ar1[] and right side of ar2[] ` ` ` `l ` `=` `0` ` ` `r ` `=` `n` `-` `1` ` ` `while` `(l < m ` `and` `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` `=` `r` `-` `1` ` ` `else` `: ` `# move to the greater side ` ` ` `l` `=` `l` `+` `1` ` ` ` ` ` ` `# Print the result ` ` ` `print` `(` `"The closest pair is ["` `, ` ` ` `ar1[res_l],` `","` `,ar2[res_r],` `"]"` `) ` ` ` `# Driver program to test above functions ` `ar1 ` `=` `[` `1` `, ` `4` `, ` `5` `, ` `7` `] ` `ar2 ` `=` `[` `10` `, ` `20` `, ` `30` `, ` `40` `] ` `m ` `=` `len` `(ar1) ` `n ` `=` `len` `(ar2) ` `x ` `=` `38` `printClosest(ar1, ar2, m, n, x) ` ` ` `# This code is contributed by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find closest pair in ` `// an array ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// ar1[0..m-1] and ar2[0..n-1] are two ` ` ` `// given sorted arrays/ and x is given ` ` ` `// number. This function prints the ` ` ` `// pair from both arrays such that the ` ` ` `// sum of the pair is closest to x. ` ` ` `static` `void` `printClosest(` `int` `[]ar1, ` ` ` `int` `[]ar2, ` `int` `m, ` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` ` ` `// Initialize the diff between pair ` ` ` `// sum and x. ` ` ` `int` `diff = ` `int` `.MaxValue; ` ` ` ` ` `// res_l and res_r are result ` ` ` `// indexes from ar1[] and ar2[] ` ` ` `// respectively ` ` ` `int` `res_l = 0, res_r = 0; ` ` ` ` ` `// 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` `(Math.Abs(ar1[l] + ` ` ` `ar2[r] - x) < diff) ` ` ` `{ ` ` ` `res_l = l; ` ` ` `res_r = r; ` ` ` `diff = Math.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 ` ` ` `Console.Write(` `"The closest pair is ["` ` ` `+ ar1[res_l] + ` `", "` ` ` `+ ar2[res_r] + ` `"]"` `); ` ` ` `} ` ` ` ` ` `// Driver program to test above functions ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]ar1 = {1, 4, 5, 7}; ` ` ` `int` `[]ar2 = {10, 20, 30, 40}; ` ` ` `int` `m = ar1.Length; ` ` ` `int` `n = ar2.Length; ` ` ` `int` `x = 38; ` ` ` ` ` `printClosest(ar1, ar2, m, n, x); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the pair ` `// from two sorted arays such ` `// that the sum of pair is ` `// closest to a given number x ` ` ` `// ar1[0..m-1] and ar2[0..n-1] ` `// are two given sorted arrays ` `// and x is given number. This ` `// function prints the pair from ` `// both arrays such that the sum ` `// of the pair is closest to x. ` `function` `printClosest(` `$ar1` `, ` `$ar2` `, ` ` ` `$m` `, ` `$n` `, ` `$x` `) ` `{ ` ` ` ` ` `// Initialize the diff between ` ` ` `// pair sum and x. ` ` ` `$diff` `= PHP_INT_MAX; ` ` ` ` ` `// res_l and res_r are result ` ` ` `// indexes from ar1[] and ar2[] ` ` ` `// respectively ` ` ` `$res_l` `; ` ` ` `$res_r` `; ` ` ` ` ` `// Start from left side of ` ` ` `// ar1[] and right side of ar2[] ` ` ` `$l` `= 0; ` ` ` `$r` `= ` `$n` `- 1; ` ` ` `while` `(` `$l` `< ` `$m` `and` `$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` `--; ` ` ` ` ` `// move to the greater side ` ` ` `else` ` ` `$l` `++; ` ` ` `} ` ` ` ` ` `// Print the result ` ` ` `echo` `"The closest pair is ["` `, ` `$ar1` `[` `$res_l` `] , ` `", "` ` ` `, ` `$ar2` `[` `$res_r` `] , ` `"] \n"` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$ar1` `= ` `array` `(1, 4, 5, 7); ` ` ` `$ar2` `= ` `array` `(10, 20, 30, 40); ` ` ` `$m` `= ` `count` `(` `$ar1` `); ` ` ` `$n` `= ` `count` `(` `$ar2` `); ` ` ` `$x` `= 38; ` ` ` `printClosest(` `$ar1` `, ` `$ar2` `, ` `$m` `, ` `$n` `, ` `$x` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

The closest pair is [7, 30]

Smallest Difference pair of values between two unsorted Arrays

This article is contributed by Harsh. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Find three closest elements from given three sorted arrays
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Given a sorted and rotated array, find if there is a pair with a given sum
- Find Sum of pair from two arrays with maximum sum
- Python List Comprehension to find pair with given sum from two arrays
- Find a pair of elements swapping which makes sum of two arrays same
- Closest product pair in an array
- Find m-th smallest value in k sorted arrays
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Find common elements in three sorted arrays
- Find relative complement of two sorted arrays
- Pair with given product | Set 1 (Find if any pair exists)
- Merge k sorted arrays | Set 2 (Different Sized Arrays)
- Find k closest elements to a given value
- Find the Sub-array with sum closest to 0