Minimum number of coins to be collected per hour to empty N piles in at most H hours | Function to find the minimum number of coins to be collected per hour to empty N piles in H hours ; Stores the minimum coins to be removed per hour ; Find the maximum array element ; Perform Binary Search ; Store the mid value of the range in K ; Find the total time taken to empty N piles by removing K coins per hour ; If total time does not exceed H ; Otherwise ; Print the required result ; Driver Code ; Function Call
function minCollectingSpeed ( piles , H ) { var ans = - 1 ; var low = 1 , high ; high = piles . reduce ( ( a , b ) => Math . max ( a , b ) ) ; while ( low <= high ) { var K = low + parseInt ( ( high - low ) / 2 ) ; var time = 0 ; piles . forEach ( ai => { time += parseInt ( ( ai + K - 1 ) / K ) ; } ) ; if ( time <= H ) { ans = K ; high = K - 1 ; } else { low = K + 1 ; } } document . write ( ans ) ; } var arr = [ 3 , 6 , 7 , 11 ] ; var H = 8 ; minCollectingSpeed ( arr , H ) ;