Minimum absolute value of ( K – arr [ i ] ) for all possible values of K over the range [ 0 , N – 1 ] | Function to find the minimum absolute value of ( K - arr [ i ] ) for each possible value of K over the range [ 0 , N - 1 ] | ; Traverse the given array arr [ ] ; Stores the mininimum distance to any array element arr [ i ] ; Check if there is no safe position smaller than i ; Check if the current position is between two safe positions ; Take the minimum of two distances ; Check if the current index is a safe position ; Check if there is no safe position greater than i ; Print the minimum distance ; Driver Code
def minimumDistance ( arr , N ) : NEW_LINE INDENT ind = 0 ; NEW_LINE prev = arr [ ind ] ; NEW_LINE s = len ( arr ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT distance = 10 ** 9 ; NEW_LINE if ( i < arr [ 0 ] ) : NEW_LINE INDENT distance = arr [ 0 ] - i ; NEW_LINE DEDENT elif ( i >= prev and ind + 1 < s and i <= arr [ ind + 1 ] ) : NEW_LINE INDENT distance = min ( i - prev , arr [ ind + 1 ] - i ) ; NEW_LINE if ( i == arr [ ind + 1 ] ) : NEW_LINE INDENT distance = 0 ; NEW_LINE prev = arr [ ind + 1 ] ; NEW_LINE ind += 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT distance = i - prev ; NEW_LINE DEDENT print ( distance , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT N = 5 ; NEW_LINE arr = [ 0 , 4 ] ; NEW_LINE minimumDistance ( arr , N ) ; NEW_LINE