Maximum difference between two elements in an Array | Function to return the maximum absolute difference between any two elements of the array ; To store the minimum and the maximum elements from the array ; Driver code
function maxAbsDiff ( arr , n ) { let minEle = arr [ 0 ] ; let maxEle = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { minEle = Math . min ( minEle , arr [ i ] ) ; maxEle = Math . max ( maxEle , arr [ i ] ) ; } return ( maxEle - minEle ) ; } let arr = [ 2 , 1 , 5 , 3 ] ; let n = arr . length ; document . write ( maxAbsDiff ( arr , n ) ) ;