Smallest possible integer K such that ceil of each Array element when divided by K is at most M | Function to check if the sum of ceil values of the arr [ ] for the K value is at most M or not ; Stores the sum of ceil values ; Update the sum ; Return true if sum is less than or equal to M , false otherwise ; Function to find the smallest possible integer K such that ceil ( arr [ 0 ] / K ) + ceil ( arr [ 1 ] / K ) + ... . + ceil ( arr [ N - 1 ] / K ) is less than or equal to M ; Stores the low value ; Stores the high value ; Stores the middle value ; Update the mid value ; Check if the mid value is K ; Update the low value ; Update the high value ; Check if low is K or high is K and return it ; Driver Code
function isvalid ( arr , K , N , M ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += Math . ceil ( arr [ i ] * 1.0 / K ) ; } return sum <= M ; } function smallestValueForK ( arr , N , M ) { let low = 1 ; let high = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { high = Math . max ( high , arr [ i ] ) ; } let mid ; while ( high - low > 1 ) { mid = ( high + low ) / 2 ; if ( ! isvalid ( arr , mid , N , M ) ) low = mid + 1 ; else high = mid ; } return isvalid ( arr , low , N , M ) ? low : high ; } let arr = [ 4 , 3 , 2 , 7 ] ; let N = arr . length ; let M = 5 ; document . write ( smallestValueForK ( arr , N , M ) ) ;