Minimum days to make Array elements with value at least K sum at least X | Function to find the minimum number of days such that the sum of array elements >= K is at least X ; Initialize the boundaries of search space ; Perform the binary search ; Find the value of mid ; Traverse the array , arr [ ] ; Find the value of arr [ i ] after mid number of days ; Check if temp is not less than K ; Update the value of sum ; Check if the value of sum is greater than X ; Update value of high ; Update the value of low ; Print the minimum number of days ; Driver Code
function findMinDays ( arr , R , N , X , K ) { let low = 0 , high = X ; let minDays ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let temp = arr [ i ] + R [ i ] * mid ; if ( temp >= K ) { sum += temp ; } } if ( sum >= X ) { minDays = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } document . write ( minDays ) ; } let X = 100 , K = 45 ; let arr = [ 2 , 5 , 2 , 6 ] ; let R = [ 10 , 13 , 15 , 12 ] ; let N = arr . length findMinDays ( arr , R , N , X , K ) ;