Minimum time remaining for safety alarm to start | Function to check if the value of mid as the minimum number of hours satisfies the condition ; Stores the sum of speed ; Iterate over the range [ 0 , N ] ; Find the value of speed ; If the bike is considered to be fast add it in sum ; Return the resultant sum ; Function to find the minimum number of time required ; Stores the range of Binary Search ; Stores the minimum number of time required ; Find the value of mid ; If the mid is the resultant speed required ; Update the ans and high ; Otherwise ; Return the minimum number of hours ; Driver Code
function check ( H , A , mid , N , M , L ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let speed = mid * A [ i ] + H [ i ] ; if ( speed >= L ) { sum += speed ; } } return sum ; } function buzzTime ( N , M , L , H , A ) { let low = 0 , high = 1e10 ; let ans = 0 ; while ( high >= low ) { let mid = Math . floor ( low + ( high - low ) / 2 ) ; if ( check ( H , A , mid , N , M , L ) >= M ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } let M = 400 , L = 120 ; let H = [ 20 , 50 , 20 ] ; let A = [ 20 , 70 , 90 ] ; let N = A . length ; document . write ( buzzTime ( N , M , L , H , A ) ) ;