Minimum time remaining for safety alarm to start
Last Updated :
16 Aug, 2021
Geek is organizing a bike race with N bikers. The initial speed of the ith biker is denoted by Hi Km/hr and the acceleration of the ith biker as Ai Km/Hr2. A biker whose speed is ‘L’ or more, is considered be a fast biker. The total speed on the track for every hour is calculated by adding the speed of each fast biker in that hour. When the total speed on the track is ‘M’ kilometers per hour or more, the safety alarm turns on. The task is to find the minimum number of hours after which the safety alarm will start.
Examples:
Input: N = 3, M = 400, L = 120, H = {20, 50, 20}, A = {20, 70, 90}
Output: 3
Explanation:
Speeds of all the Bikers at ith hour:
Biker1 = [20 40 60 80 100]
Biker2 = [50 120 190 260 330]
Biker3 = [20 110 200 290 380].
Initial Speed on track = 0, because none of the biker’s speed is fast enough.
Speed on track after 1st Hour = 120.
Speed on track after 2nd Hour = 190 + 200 = 390.
Speed on track after 3rd Hour = 260 + 290.
Alarm will start at the 3rd hour.
Input: N = 2, M = 60, L = 120, H = {50, 30}, A = {20, 40}
Output: 3
Approach: The given problem can be solved by using Binary Search by using the fact that if the bikes have an initial speed U and have uniform acceleration A then the speed at any point of time can be found using the equation: (V = U + A*t) and if, at a time t, the conditions satisfy, then for all time greater than t, will satisfy so discard the right half of the range until found the minimum value. Follow the steps below to solve the problem:
- Define a function check(long H[], long A[], long mid, long N, long M, long L) and perform the following steps:
- Initialize the variable, say sum as 0 that stores the sum of speeds.
- Iterate over a range [0, N] using the variable i and if the value of (mid*A[i] + H[i]) is at least L, then add this value to the sum.
- After performing the above steps, return the value of the sum as the result.
- Initialize the variables, say low as 0 and high as 1010 as the range of the binary search of the answer and ans as 0 that stores the minimum number of hours.
- Iterate until low <= high and perform the following steps:
- Find the value of mid as (low + high)/2.
- Call the function check(H, A, mid, N, M, L) and if the value returned by the function is at least M, then update the value of ans as mid. Otherwise, update the value of high as (mid – 1).
- Otherwise, update the value of low as (mid + 1).
- After performing the above steps, print the value of ans as the resultant number of hours.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long check( long H[], long A[], long mid,
long N, long M, long L)
{
long sum = 0;
for ( long i = 0; i < N; i++) {
long speed = mid * A[i] + H[i];
if (speed >= L) {
sum += speed;
}
}
return sum;
}
long buzzTime( long N, long M, long L,
long H[], long A[])
{
long low = 0, high = 1e10;
long ans = 0;
while (high >= low) {
long mid = low + (high - low) / 2;
if (check(H, A, mid,
N, M, L)
>= M) {
ans = mid;
high = mid - 1;
}
else
low = mid + 1;
}
return ans;
}
int main()
{
long M = 400, L = 120;
long H[] = { 20, 50, 20 };
long A[] = { 20, 70, 90 };
long N = sizeof (A) / sizeof (A[0]);
cout << buzzTime(N, M, L, H, A);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static long check( long H[], long A[], long mid,
long N, long M, long L)
{
long sum = 0 ;
for ( long i = 0 ; i < N; i++) {
long speed = mid * A[( int ) i] + H[( int ) i];
if (speed >= L) {
sum += speed;
}
}
return sum;
}
static long buzzTime( long N, long M, long L,
long H[], long A[])
{
long low = 0 , high = 100000000 ;
long ans = 0 ;
while (high >= low) {
long mid = low + (high - low) / 2 ;
if (check(H, A, mid,
N, M, L)
>= M) {
ans = mid;
high = mid - 1 ;
}
else
low = mid + 1 ;
}
return ans;
}
public static void main (String[] args) {
long M = 400 , L = 120 ;
long H[] = { 20 , 50 , 20 };
long A[] = { 20 , 70 , 90 };
long N = A.length;
System.out.println(buzzTime(N, M, L, H, A));
}
}
|
Python3
def check(H, A, mid, N, M, L):
sum = 0
for i in range (N):
speed = mid * A[i] + H[i]
if (speed > = L):
sum + = speed
return sum
def buzzTime(N, M, L, H, A):
low = 0
high = 1e10
ans = 0
while (high > = low):
mid = low + (high - low) / / 2
if (check(H, A, mid, N, M, L) > = M):
ans = mid
high = mid - 1
else :
low = mid + 1
return int (ans)
if __name__ = = '__main__' :
M = 400
L = 120
H = [ 20 , 50 , 20 ]
A = [ 20 , 70 , 90 ]
N = len (A)
print (buzzTime(N, M, L, H, A))
|
C#
using System;
class GFG{
static long check( long []H, long []A, long mid,
long N, long M, long L)
{
long sum = 0;
for ( long i = 0; i < N; i++)
{
long speed = mid * A[( int ) i] + H[( int ) i];
if (speed >= L)
{
sum += speed;
}
}
return sum;
}
static long buzzTime( long N, long M, long L,
long []H, long []A)
{
long low = 0, high = 100000000;
long ans = 0;
while (high >= low)
{
long mid = low + (high - low) / 2;
if (check(H, A, mid, N, M, L) >= M)
{
ans = mid;
high = mid - 1;
}
else
low = mid + 1;
}
return ans;
}
public static void Main(String[] args)
{
long M = 400, L = 120;
long []H = { 20, 50, 20 };
long []A = { 20, 70, 90 };
long N = A.Length;
Console.Write(buzzTime(N, M, L, H, A));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity:O(N*log(max(L, M)))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...