Maximize minimum array element possible by exactly K decrements | Function to find the maximized minimum element of the array after performing given operation exactly K times ; Stores the minimum element ; Traverse the given array ; Update the minimum element ; Stores the required operations to make all elements equal to the minimum element ; Update required operations ; If reqOperations < K ; Decrement the value of K by reqOperations ; Update minElement ; Return minimum element ; Driver Code
function minimumElement ( arr , N , K ) { let minElement = arr [ 0 ] ; for ( let i = 0 ; i < N ; ++ i ) { minElement = Math . min ( minElement , arr [ i ] ) ; } let reqOperations = 0 ; for ( let i = 0 ; i < N ; ++ i ) { reqOperations += arr [ i ] - minElement ; } if ( reqOperations < K ) { K -= reqOperations ; minElement -= Math . floor ( ( K + N - 1 ) / N ) ; } return minElement ; } let arr = [ 10 , 10 , 10 , 10 ] ; let K = 7 ; let N = arr . length ; document . write ( minimumElement ( arr , N , K ) ) ;