Smallest number to make Array sum at most K by dividing each element | Function to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Binary search between 1 and 10 ^ 9 ; Calculating the new sum after dividing every element by mid ; If after dividing by mid , if the new sum is less than or equal to limit move low to mid + 1 ; Else , move mid + 1 to high ; Returning the minimum number ; Driver Code
function findMinDivisor ( arr , n , limit ) { let low = 0 , high = 1000000000 ; while ( low < high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += Math . ceil ( arr [ i ] / mid ) ; } if ( sum <= limit ) high = mid ; else low = mid + 1 ; } return low ; } let arr = [ 2 , 3 , 4 , 9 ] ; let N = arr . length ; let K = 6 ; document . write ( findMinDivisor ( arr , N , K ) ) ;