Queries to find minimum absolute difference between adjacent array elements in given ranges | JavaScript 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
let MAX = 5000 ; function minDifference ( arr , n , q , m ) { for ( let i = 0 ; i < m ; i ++ ) { let L = q [ i ] [ 0 ] , R = q [ i ] [ 1 ] ; let ans = MAX ; for ( let i = L ; i < R ; i ++ ) { ans = Math . min ( ans , arr [ i ] ) ; } document . write ( ans + ' ' ) ; } } function minimumDifference ( arr , q , N , m ) { let diff = new Array ( N ) ; for ( let i = 0 ; i < N - 1 ; i ++ ) diff [ i ] = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; minDifference ( diff , N - 1 , q , m ) ; } let arr = [ 2 , 6 , 1 , 8 , 3 , 4 ] ; let N = arr . length ; let Q = [ [ 0 , 3 ] , [ 1 , 5 ] , [ 4 , 5 ] ] ; let M = Q . length ; minimumDifference ( arr , Q , N , M ) ;