Pair with given sum and maximum shortest distance from end | C # code to find maximum shortest distance from endpoints ; Store counts of all elements . ; function to find maximum shortest distance ; stores the shortest distance of every element in original array . ; shortest distance from ends ; if duplicates are found , b [ x ] is replaced with minimum of the previous and current position 's  shortest distance ; similar elements ignore them cause we need distinct elements ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void makePermutation ( int [ ] a , int n ) { Dictionary < int , int > count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( count . ContainsKey ( a [ i ] ) ) { count [ a [ i ] ] = count [ a [ i ] ] + 1 ; } else { count . Add ( a [ i ] , 1 ) ; } } } static int find_maximum ( int [ ] a , int n , int k ) { Dictionary < int , int > b = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; int d = Math . Min ( 1 + i , n - i ) ; if ( ! b . ContainsKey ( x ) ) b . Add ( x , d ) ; else { b [ x ] = Math . Min ( d , b [ x ] ) ; } } int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( x != k - x && b . ContainsKey ( k - x ) ) ans = Math . Min ( Math . Max ( b [ x ] , b [ k - x ] ) , ans ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 3 , 5 , 8 , 6 , 7 } ; int K = 11 ; int n = a . Length ; Console . WriteLine ( find_maximum ( a , n , K ) ) ; } }