# Pair with given sum and maximum shortest distance from end

Given an array of N integers and an integer K, pick two distinct elements whose sum is K and find the maximum shortest distance of the picked elements from the endpoints.

**Examples:**

Input : a[] = {2, 4, 3, 2, 1} k = 5. Output : 2 Explanation: Select the pair(4, 1). Shortest distance of 4 from ends = 2 Shortest distance of 1 from ends = 1 Hence, answer is max(2, 1) = 2 Input : a[] = {2, 4, 1, 9, 5} k = 3 Output : 3 Explanation: Select the pair (2, 1) Shortest distance of 2 from ends = 1 Shortest distance of 1 from ends = 3 Hence, answer is max(1, 3) = 3.

**Note:** The distance of end elements from ends is 1 and not 0.

**Naive approach:** The approach is to run two loops and in inner loop check if two elements are making a pair with sum k. If yes, then make answer as maximum of the shortest distances of two elements, compare it with the previous pair’s answer and make answer as minimum of these two. When the loop ends we get the desired output.

**Efficient Approach:** Clearly, Shortest distance is the distance from left end and distance from right end i.e, . Let us denote shortest distance of i-th element as . There is another case where an element in the selected pair is repeated then select minimum of all the shortest distances of occurrences of that element. Run a loop and store shortest distance of all the array elements in another array(let it be ). Now, we got shortest distances of all the elements.

Run a for loop. If the picked element is **x**, then the other element should be **k-x**. Update the ans with and at every update, select the minimum of previous and present answer. If** k-x** is not in the array, then will be INFINITE, which will be initialized already.

## C++

`// C++ code to find maximum shortest distance ` `// from endpoints ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find maximum shortest distance ` `int` `find_maximum(` `int` `a[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// stores the shortest distance of every ` ` ` `// element in original array. ` ` ` `unordered_map<` `int` `, ` `int` `> b; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `int` `x = a[i]; ` ` ` ` ` `// shortest distance from ends ` ` ` `int` `d = min(1 + i, n - i); ` ` ` `if` `(b.find(x) == b.end()) ` ` ` `b[x] = d; ` ` ` ` ` `else` ` ` ` ` `/* if duplicates are found, b[x] ` ` ` `is replaced with minimum of the ` ` ` `previous and current position's ` ` ` `shortest distance*/` ` ` `b[x] = min(d, b[x]); ` ` ` `} ` ` ` ` ` `int` `ans = INT_MAX; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `int` `x = a[i]; ` ` ` ` ` `// similar elements ignore them ` ` ` `// cause we need distinct elements ` ` ` `if` `(x != k - x && b.find(k - x) != b.end()) ` ` ` `ans = min(max(b[x], b[k - x]), ans); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 3, 5, 8, 6, 7 }; ` ` ` `int` `K = 11; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << find_maximum(a, n, K) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find maximum shortest ` `# distance from endpoints ` ` ` `# function to find maximum shortest distance ` `def` `find_maximum(a, n, k): ` ` ` ` ` `# stores the shortest distance of every ` ` ` `# element in original array. ` ` ` `b ` `=` `dict` `() ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `x ` `=` `a[i] ` ` ` ` ` `# shortest distance from ends ` ` ` `d ` `=` `min` `(` `1` `+` `i, n ` `-` `i) ` ` ` `if` `x ` `not` `in` `b.keys(): ` ` ` `b[x] ` `=` `d ` ` ` `else` `: ` ` ` ` ` `# if duplicates are found, b[x] ` ` ` `# is replaced with minimum of the ` ` ` `# previous and current position's ` ` ` `# shortest distance*/ ` ` ` `b[x] ` `=` `min` `(d, b[x]) ` ` ` ` ` `ans ` `=` `10` `*` `*` `9` ` ` `for` `i ` `in` `range` `(n): ` ` ` `x ` `=` `a[i] ` ` ` ` ` `# similar elements ignore them ` ` ` `# cause we need distinct elements ` ` ` `if` `(x !` `=` `(k ` `-` `x) ` `and` `(k ` `-` `x) ` `in` `b.keys()): ` ` ` `ans ` `=` `min` `(` `max` `(b[x], b[k ` `-` `x]), ans) ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `a ` `=` `[` `3` `, ` `5` `, ` `8` `, ` `6` `, ` `7` `] ` `K ` `=` `11` `n ` `=` `len` `(a) ` `print` `(find_maximum(a, n, K)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Probability of a random pair being the maximum weighted pair
- Pair formation such that maximum pair sum is minimized
- Minimum distance between two occurrences of maximum
- Maximum distance between two unequal elements
- Maximum AND value of a pair in an array
- Find maximum distance between any city and station
- Maximum distance between two occurrences of same element in array
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array
- Find a rotation with maximum hamming distance
- Print pair with maximum AND value in an array
- Find Sum of pair from two arrays with maximum sum
- Rectangle with Maximum Area using Java Pair
- Find pair with maximum difference in any column of a Matrix
- Find a pair with maximum product in array of Integers
- Count ways of choosing a pair with maximum difference

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.