Find the maximum possible value of the minimum value of modified array | Function to find the maximum possible value of the minimum value of the modified array ; To store minimum value of array ; To store sum of elements of array ; Solution is not possible ; zero is the possible value ; minimum possible value ; maximum possible value ; to store a required answer ; Binary Search ; If mid is possible then try to increase required answer ; If mid is not possible then decrease required answer ; Return required answer ; Driver Code
< ? php function maxOfMin ( $ a , $ n , $ S ) { $ mi = PHP_INT_MAX ; $ s1 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ s1 += $ a [ $ i ] ; $ mi = min ( $ a [ $ i ] , $ mi ) ; } if ( $ s1 < $ S ) return -1 ; if ( $ s1 == $ S ) return 0 ; $ low = 0 ; $ high = $ mi ; $ ans ; while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) / 2 ; if ( $ s1 - ( $ mid * $ n ) >= $ S ) { $ ans = $ mid ; $ low = $ mid + 1 ; } else $ high = $ mid - 1 ; } return $ ans ; } $ a = array ( 10 , 10 , 10 , 10 , 10 ) ; $ S = 10 ; $ n = sizeof ( $ a ) ; echo maxOfMin ( $ a , $ n , $ S ) ; ? >