Queries to find minimum absolute difference between adjacent array elements in given ranges | Python3 program for the above approach ; Function to find the minimum difference between adjacent array element over the given range [ L , R ] for Q Queries ; Find the sum of all queries ; Left and right boundaries of current range ; Print the sum of the current query range ; Function to find the minimum absolute difference of adjacent array elements for the given range ; Stores the absolute difference of adjacent elements ; Find the minimum difference of adjacent elements ; Driver Code
MAX = 5000 ; NEW_LINE def minDifference ( arr , n , q , m ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT L = q [ i ] [ 0 ] ; R = q [ i ] [ 1 ] ; NEW_LINE ans = MAX ; NEW_LINE for i in range ( L , R ) : NEW_LINE INDENT ans = min ( ans , arr [ i ] ) ; NEW_LINE DEDENT print ( ans ) ; NEW_LINE DEDENT DEDENT def minimumDifference ( arr , q , N , m ) : NEW_LINE INDENT diff = [ 0 ] * N ; NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT diff [ i ] = abs ( arr [ i ] - arr [ i + 1 ] ) ; NEW_LINE DEDENT minDifference ( diff , N - 1 , q , m ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 6 , 1 , 8 , 3 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE Q = [ [ 0 , 3 ] , [ 1 , 5 ] , [ 4 , 5 ] ] ; NEW_LINE M = len ( Q ) ; NEW_LINE minimumDifference ( arr , Q , N , M ) ; NEW_LINE DEDENT