Smallest positive integer that divides all array elements to generate quotients with sum not exceeding K | Function to find the smallest positive integer that divides array elements to get the sum <= K ; Stores minimum possible of the smallest positive integer satisfying the condition ; Stores maximum possible of the smallest positive integer satisfying the condition ; Apply binary search over the range [ left , right ] ; Stores middle element of left and right ; Stores sum of array elements ; Traverse the array ; Update sum ; If sum is greater than K ; Update left ; Update right ; Driver Code
function findSmallestInteger ( arr , N , K ) { var left = 1 ; var right = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; while ( left < right ) { var mid = ( left + right ) / 2 ; var sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { sum += parseInt ( ( arr [ i ] + mid - 1 ) / mid ) ; } if ( sum > K ) { left = mid + 1 ; } else { right = mid ; } } return left ; } var arr = [ 1 , 2 , 5 , 9 ] ; var N = arr . length ; var K = 6 ; document . write ( findSmallestInteger ( arr , N , K ) ) ;