Minimize the difference between the maximum and minimum values of the modified array | Function to return required minimum difference ; finding minimum and maximum values ; returning minimum possible difference ; Driver program ; function to return the answer
function minDiff ( n , x , A ) { var mn = A [ 0 ] , mx = A [ 0 ] ; for ( var i = 0 ; i < n ; ++ i ) { mn = Math . min ( mn , A [ i ] ) ; mx = Math . max ( mx , A [ i ] ) ; } return Math . max ( 0 , mx - mn - 2 * x ) ; } var n = 3 , x = 3 ; var A = [ 1 , 3 , 6 ] ; document . write ( minDiff ( n , x , A ) ) ;