Maximum difference between a pair of adjacent elements by excluding every element once | Function to calculate maximum difference between adjacent elements excluding every array element once ; Compute maximum adjacent difference for whole array ; Store the maximum between arr_max and curr_max ; Append the result into a vector ; Print the result ; Driver Code
function maxAdjacent ( arr , N ) { let res = [ ] ; let arr_max = Number . MIN_VALUE ; for ( let i = 1 ; i < N ; i ++ ) { arr_max = Math . max ( arr_max , Math . abs ( arr [ i - 1 ] - arr [ i ] ) ) ; } for ( let i = 1 ; i < N - 1 ; i ++ ) { let curr_max = Math . abs ( arr [ i - 1 ] - arr [ i + 1 ] ) ; let ans = Math . max ( curr_max , arr_max ) ; res . push ( ans ) ; } document . write ( res . join ( " " ) ) ; } let arr = [ 1 , 3 , 4 , 7 , 8 ] ; let N = arr . length ; maxAdjacent ( arr , N ) ;