Minimum distance between any special pair in the given array | ''Python3 program for the above approach ; ''Function that finds the minimum difference between two vectors ; '' Find lower bound of the index ; '' Find two adjacent indices  to take difference ; '' Return the result ; ''Function to find the minimum distance between index of special pairs ; '' Stores the index of each element  in the array arr[] ; Store the indexes ; Get the unique values in list ; '' Take adjacent difference  of same values ; '' Left index array ; '' Right index array ; '' Find the minimum gap between  the two adjacent different  values ; ''Driver Code ; '' Given array  ; '' Function call
import sys NEW_LINE def mindist ( left , right ) : NEW_LINE INDENT res = sys . maxsize NEW_LINE for i in range ( len ( left ) ) : NEW_LINE INDENT num = left [ i ] NEW_LINE index = right . index ( min ( [ i for i in right if num >= i ] ) ) NEW_LINE if ( index == 0 ) : NEW_LINE INDENT res = min ( res , abs ( num - right [ index ] ) ) NEW_LINE DEDENT elif ( index == len ( right ) ) : NEW_LINE INDENT res = min ( res , min ( abs ( num - right [ index - 1 ] ) , abs ( num - right [ index ] ) ) ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def specialPairs ( nums ) : NEW_LINE INDENT m = { } NEW_LINE vals = [ ] NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT m [ nums [ i ] ] = i NEW_LINE DEDENT for p in m : NEW_LINE INDENT vals . append ( p ) NEW_LINE DEDENT res = sys . maxsize NEW_LINE for i in range ( 1 , len ( vals ) ) : NEW_LINE INDENT vec = [ m [ vals [ i ] ] ] NEW_LINE for i in range ( 1 , len ( vec ) ) : NEW_LINE INDENT res = min ( res , abs ( vec [ i ] - vec [ i - 1 ] ) ) NEW_LINE DEDENT if ( i ) : NEW_LINE INDENT a = vals [ i ] NEW_LINE left = [ m [ a ] ] NEW_LINE b = vals [ i - 1 ] NEW_LINE right = [ m [ b ] ] NEW_LINE res = min ( res , mindist ( left , right ) ) + 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , - 10 , 5 , - 5 , 1 ] NEW_LINE print ( specialPairs ( arr ) ) NEW_LINE DEDENT