Maximize difference between maximum and minimum array elements after K operations | Reverse array ; Function to find the maximum difference between the maximum and minimum in the array after K operations ; Stores maximum difference between largest and smallest array element ; Sort the array in descending order ; Traverse the array arr ; Update maxDiff ; Driver Code
function reverse ( a ) { var i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } function maxDiffLargSmallOper ( arr , N , K ) { var maxDiff = 0 ; arr . sort ( ) ; arr = reverse ( arr ) ; for ( i = 0 ; i <= Math . min ( K , N - 1 ) ; i ++ ) { maxDiff += arr [ i ] ; } return maxDiff ; } var arr = [ 7 , 7 , 7 , 7 ] ; var N = arr . length ; var K = 1 ; document . write ( maxDiffLargSmallOper ( arr , N , K ) ) ;