Maximize minimum array element possible by exactly K decrements | Java program for the above approach ; 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
import java . io . * ; class GFG { static int minimumElement ( int arr [ ] , int N , int K ) { int minElement = arr [ 0 ] ; for ( int i = 0 ; i < N ; ++ i ) { minElement = Math . min ( minElement , arr [ i ] ) ; } int reqOperations = 0 ; for ( int i = 0 ; i < N ; ++ i ) { reqOperations += arr [ i ] - minElement ; } if ( reqOperations < K ) { K -= reqOperations ; minElement -= ( K + N - 1 ) / N ; } return minElement ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 10 , 10 , 10 } ; int K = 7 ; int N = arr . length ; System . out . println ( minimumElement ( arr , N , K ) ) ; } }