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

## 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*

**Output:**

The closest pair is [7, 30]

Please refer complete article on Find the closest pair from two sorted arrays for more details!

## Recommended Posts:

- C# Program for Median of two sorted arrays of same size
- C# Program to find whether a no is power of two
- C# Program to Find the Number Occurring Odd Number of Times
- Program to find absolute value of a given number
- C# Program for Naive algorithm for Pattern Searching
- C# Program for KMP Algorithm for Pattern Searching
- C# Program to print all permutations of a given string
- C# Program for Count Inversions in an array | Set 1 (Using Merge Sort)
- C# Program for Activity Selection Problem | Greedy Algo-1
- C# Program for Subset Sum Problem | DP-25
- C# Program for Dijkstra's shortest path algorithm | Greedy Algo-7
- C# Program for Maximum sum rectangle in a 2D matrix | DP-27
- C# Program for Largest Sum Contiguous Subarray
- C# Program for Maximum size square sub-matrix with all 1s
- C# Program for Minimum number of jumps to reach end
- C# Program for Cutting a Rod | DP-13
- C# Program for Longest Palindromic Subsequence | DP-12
- C# Program for Egg Dropping Puzzle | DP-11
- C# Program for Matrix Chain Multiplication | DP-8
- C# Program for Count set bits in an integer