Minimize maximum array element possible by at most K splits on the given array | Function to check if all array elements can be reduced to at most mid by at most K splits ; Stores the number of splits required ; Traverse the array arr [ ] ; Update count ; If possible , return true . Otherwise return false ; Function to find the minimum possible value of maximum array element that can be obtained by at most K splits ; Set lower and upper limits ; Perform Binary Search ; Calculate mid ; Check if all array elements can be reduced to at most mid value by at most K splits ; Update the value of hi ; Otherwise ; Update the value of lo ; Return the minimized maximum element in the array ; Driver Code
function possible ( A , N , mid , K ) { var count = 0 ; var i ; for ( i = 0 ; i < N ; i ++ ) { count += Math . floor ( ( A [ i ] - 1 ) / mid ) ; } if ( count <= K ) return true ; else return false } function minimumMaximum ( A , N , K ) { var lo = 1 ; var hi = Math . max . apply ( Math , A ) ; var mid ; while ( lo < hi ) { mid = Math . floor ( ( lo + hi ) / 2 ) ; if ( possible ( A , N , mid , K ) ) { hi = mid ; } else { lo = mid + 1 ; } } return hi ; } var arr = [ 2 , 4 , 8 , 2 ] ; var K = 4 ; var N = arr . length ; document . write ( minimumMaximum ( arr , N , K ) ) ;