# C# Program to Find the closest pair from two sorted arrays

• Last Updated : 27 May, 2022

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

Output:

`The closest pair is [7, 30]`

Time Complexity: O(m + n), where m and n represents the size of the given two arrays.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

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

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.77186793,"math_prob":0.98986626,"size":2191,"snap":"2022-40-2023-06","text_gpt3_token_len":757,"char_repetition_ratio":0.12299954,"word_repetition_ratio":0.06550218,"special_character_ratio":0.39799178,"punctuation_ratio":0.1750503,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9990693,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-04T06:08:50Z\",\"WARC-Record-ID\":\"<urn:uuid:84704c61-000b-4eaa-91b7-e142a05c2d87>\",\"Content-Length\":\"136652\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f211a879-2a3f-44a9-b23a-092d38ea3a22>\",\"WARC-Concurrent-To\":\"<urn:uuid:a03475ef-f82c-41eb-9872-770442c8ed8a>\",\"WARC-IP-Address\":\"23.218.218.153\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/csharp-program-for-find-the-closest-pair-from-two-sorted-arrays/\",\"WARC-Payload-Digest\":\"sha1:JCHQDNDQV2KGZNDTP573S2NDJE7YS2KH\",\"WARC-Block-Digest\":\"sha1:LMFSDINEZUSF7TZ4QQXOPCCBKWTIHB4Q\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337480.10_warc_CC-MAIN-20221004054641-20221004084641-00722.warc.gz\"}"}