Maximize the minimum array element by M subarray increments of size S | Java program for the above approach ; Function to return index of minimum element in the array ; Initialize a [ 0 ] as minValue ; Traverse the array ; If a [ i ] < existing minValue ; Return the minimum index ; Function that maximize the minimum element of array after incrementing subarray of size S by 1 , M times ; Iterating through the array or M times ; Find minimum element index ; Increment the minimum value ; Storing the left index and right index ; Incrementing S - 1 minimum elements to the left and right of minValue ; Reached extreme left ; Reached extreme right ; Left value is minimum ; Right value is minimum ; Find the minValue in A [ ] after M operations ; Return the minimum value ; Driver Code ; Function Call
import java . util . * ; class solution { static int min1 ( int a [ ] , int n ) { int minIndex = 0 , minValue = a [ 0 ] , i ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] < minValue ) { minValue = a [ i ] ; minIndex = i ; } } return minIndex ; } static int maximizeMin ( int A [ ] , int N , int S , int M ) { int minIndex , left , right , i , j ; for ( i = 0 ; i < M ; i ++ ) { minIndex = min1 ( A , N ) ; A [ minIndex ] ++ ; left = minIndex - 1 ; right = minIndex + 1 ; for ( j = 0 ; j < S - 1 ; j ++ ) { if ( left == - 1 ) A [ right ++ ] ++ ; else if ( right == N ) A [ left -- ] ++ ; else { if ( A [ left ] < A [ right ] ) A [ left -- ] ++ ; else A [ right ++ ] ++ ; } } } minIndex = min1 ( A , N ) ; return A [ minIndex ] ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . length ; int S = 2 , M = 3 ; System . out . print ( maximizeMin ( arr , N , S , M ) ) ; } }