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 ; Traverse the array ; Stores the maximum diff ; Check for maximum adjacent element ; Exclude current element ; Update maximum difference ; Update previous value ; Append the result into a vector ; Print the result ; Driver Code
function maxAdjacent ( arr , N ) { var res = [ ] ; for ( var i = 1 ; i < N - 1 ; i ++ ) { var prev = arr [ 0 ] ; var maxi = Number . MIN_VALUE ; for ( var j = 1 ; j < N ; j ++ ) { if ( i == j ) continue ; maxi = Math . max ( maxi , Math . abs ( arr [ j ] - prev ) ) ; prev = arr [ j ] ; } res . push ( maxi ) ; } for ( var j = 0 ; j < res . length ; j ++ ) document . write ( res [ j ] + " " ) ; document . write ( " " ) ; } var arr = [ 1 , 3 , 4 , 7 , 8 ] ; var N = arr . length ; maxAdjacent ( arr , N ) ;