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++
// C++ program for the above approachĀ
#include <bits/stdc++.h>using namespace std;Ā
// Function to check if the value of// mid as the minimum number of hours// satisfies the conditionlong check(long H[], long A[], long mid,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā long N, long M, long L){Ā Ā Ā Ā // Stores the sum of speedĀ Ā Ā Ā long sum = 0;Ā
Ā Ā Ā Ā // Iterate over the range [0, N]Ā Ā Ā Ā for (long i = 0; i < N; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Find the value of speedĀ Ā Ā Ā Ā Ā Ā Ā long speed = mid * A[i] + H[i];Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the bike is consideredĀ Ā Ā Ā Ā Ā Ā Ā // to be fast add it in sumĀ Ā Ā Ā Ā Ā Ā Ā if (speed >= L) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum += speed;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return the resultant sumĀ Ā Ā Ā return sum;}Ā
// Function to find the minimum number// of time requiredlong buzzTime(long N, long M, long L,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā long H[], long A[]){Ā Ā Ā Ā // Stores the range of Binary SearchĀ Ā Ā Ā long low = 0, high = 1e10;Ā
Ā Ā Ā Ā // Stores the minimum number ofĀ Ā Ā Ā // time requiredĀ Ā Ā Ā long ans = 0;Ā
Ā Ā Ā Ā while (high >= low) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Find the value of midĀ Ā Ā Ā Ā Ā Ā Ā long mid = low + (high - low) / 2;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the mid is the resultantĀ Ā Ā Ā Ā Ā Ā Ā // speed requiredĀ Ā Ā Ā Ā Ā Ā Ā if (check(H, A, mid,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā N, M, L)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā >= M) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the ans and highĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = mid;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā high = mid - 1;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // OtherwiseĀ Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā low = mid + 1;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return the minimum number of hoursĀ Ā Ā Ā return ans;}Ā
// Driver Codeint 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
// Java program for the above approachimport java.io.*;Ā
class GFG{Ā
Ā Ā // Function to check if the value ofĀ Ā // mid as the minimum number of hoursĀ Ā // satisfies the conditionĀ Ā static long check(long H[], long A[], long mid,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā long N, long M, long L)Ā Ā {Ā
Ā Ā Ā Ā // Stores the sum of speedĀ Ā Ā Ā long sum = 0;Ā
Ā Ā Ā Ā // Iterate over the range [0, N]Ā Ā Ā Ā for (long i = 0; i < N; i++) {Ā
Ā Ā Ā Ā Ā Ā // Find the value of speedĀ Ā Ā Ā Ā Ā long speed = mid * A[(int) i] + H[(int) i];Ā
Ā Ā Ā Ā Ā Ā // If the bike is consideredĀ Ā Ā Ā Ā Ā // to be fast add it in sumĀ Ā Ā Ā Ā Ā if (speed >= L) {Ā Ā Ā Ā Ā Ā Ā Ā sum += speed;Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return the resultant sumĀ Ā Ā Ā return sum;Ā Ā }Ā
Ā Ā // Function to find the minimum numberĀ Ā // of time requiredĀ Ā static long buzzTime(long N, long M, long L,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā long H[], long A[])Ā Ā {Ā Ā Ā Ā // Stores the range of Binary SearchĀ Ā Ā Ā long low = 0, high = 100000000;Ā
Ā Ā Ā Ā // Stores the minimum number ofĀ Ā Ā Ā // time requiredĀ Ā Ā Ā long ans = 0;Ā
Ā Ā Ā Ā while (high >= low) {Ā
Ā Ā Ā Ā Ā Ā // Find the value of midĀ Ā Ā Ā Ā Ā long mid = low + (high - low) / 2;Ā
Ā Ā Ā Ā Ā Ā // If the mid is the resultantĀ Ā Ā Ā Ā Ā // speed requiredĀ Ā Ā Ā Ā Ā if (check(H, A, mid,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā N, M, L)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā >= M) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update the ans and highĀ Ā Ā Ā Ā Ā Ā Ā ans = mid;Ā Ā Ā Ā Ā Ā Ā Ā high = mid - 1;Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā // OtherwiseĀ Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā low = mid + 1;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return the minimum number of hoursĀ Ā Ā Ā return ans;Ā Ā }Ā
Ā Ā // Driver CodeĀ Ā 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));Ā Ā }}Ā
// This code is contributed by Potta Lokesh |
Python3
# Python 3 program for the above approachĀ
# Function to check if the value of# mid as the minimum number of hours# satisfies the conditiondef check(H, A, mid, N, M, L):Ā Ā Ā Ā # Stores the sum of speedĀ Ā Ā Ā sum = 0Ā
Ā Ā Ā Ā # Iterate over the range [0, N]Ā Ā Ā Ā for i in range(N):Ā Ā Ā Ā Ā Ā Ā Ā # Find the value of speedĀ Ā Ā Ā Ā Ā Ā Ā speed = mid * A[i] + H[i]Ā
Ā Ā Ā Ā Ā Ā Ā Ā # If the bike is consideredĀ Ā Ā Ā Ā Ā Ā Ā # to be fast add it in sumĀ Ā Ā Ā Ā Ā Ā Ā if (speed >= L):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum += speedĀ
Ā Ā Ā Ā # Return the resultant sumĀ Ā Ā Ā return sumĀ
# Function to find the minimum number# of time requireddef buzzTime(N, M, L, H, A):Ā Ā Ā Ā # Stores the range of Binary SearchĀ Ā Ā Ā low = 0Ā Ā Ā Ā high = 1e10Ā
Ā Ā Ā Ā # Stores the minimum number ofĀ Ā Ā Ā # time requiredĀ Ā Ā Ā ans = 0Ā
Ā Ā Ā Ā while (high >= low):Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Find the value of midĀ Ā Ā Ā Ā Ā Ā Ā mid = low + (high - low) // 2Ā
Ā Ā Ā Ā Ā Ā Ā Ā # If the mid is the resultantĀ Ā Ā Ā Ā Ā Ā Ā # speed requiredĀ Ā Ā Ā Ā Ā Ā Ā if (check(H, A, mid, N, M, L) >= M):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Update the ans and highĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = midĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā high = mid - 1Ā
Ā Ā Ā Ā Ā Ā Ā Ā # OtherwiseĀ Ā Ā Ā Ā Ā Ā Ā else:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā low = mid + 1Ā
Ā Ā Ā Ā # Return the minimum number of hoursĀ Ā Ā Ā return int(ans)Ā
# Driver Codeif __name__ == '__main__':Ā Ā Ā Ā M = 400Ā Ā Ā Ā L = 120Ā Ā Ā Ā H = [20, 50, 20]Ā Ā Ā Ā A = [20, 70, 90]Ā Ā Ā Ā N = len(A)Ā
Ā Ā Ā Ā print(buzzTime(N, M, L, H, A))Ā Ā Ā Ā Ā Ā Ā Ā Ā # This code is contributed by ipg2016107. |
C#
// C# program for the above approachusing System;Ā
class GFG{Ā
// Function to check if the value of// mid as the minimum number of hours// satisfies the conditionstatic long check(long []H, long []A, long mid,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā long N, long M, long L){Ā
Ā Ā Ā Ā // Stores the sum of speedĀ Ā Ā Ā long sum = 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā // Iterate over the range [0, N]Ā Ā Ā Ā for(long i = 0; i < N; i++) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Find the value of speedĀ Ā Ā Ā Ā Ā Ā Ā long speed = mid * A[(int) i] + H[(int) i];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the bike is consideredĀ Ā Ā Ā Ā Ā Ā Ā // to be fast add it in sumĀ Ā Ā Ā Ā Ā Ā Ā if (speed >= L)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum += speed;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā // Return the resultant sumĀ Ā Ā Ā return sum;}Ā
// Function to find the minimum number// of time requiredstatic long buzzTime(long N, long M, long L,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā long []H, long []A){Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores the range of Binary SearchĀ Ā Ā Ā long low = 0, high = 100000000;Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores the minimum number ofĀ Ā Ā Ā // time requiredĀ Ā Ā Ā long ans = 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā while (high >= low) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Find the value of midĀ Ā Ā Ā Ā Ā Ā Ā long mid = low + (high - low) / 2;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If the mid is the resultantĀ Ā Ā Ā Ā Ā Ā Ā // speed requiredĀ Ā Ā Ā Ā Ā Ā Ā if (check(H, A, mid, N, M, L) >= M)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the ans and highĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = mid;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā high = mid - 1;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // OtherwiseĀ Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā low = mid + 1;Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā // Return the minimum number of hoursĀ Ā Ā Ā return ans;}Ā
// Driver Codepublic 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));}}Ā
// This code is contributed by shivanisinghss2110 |
Javascript
<script>Ā
// Javascript program for the above approachĀ
// Function to check if the value of// mid as the minimum number of hours// satisfies the conditionfunction check(H, A, mid, N, M, L){Ā
Ā Ā Ā Ā // Stores the sum of speedĀ Ā Ā Ā let sum = 0;Ā
Ā Ā Ā Ā // Iterate over the range [0, N]Ā Ā Ā Ā for (let i = 0; i < N; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Find the value of speedĀ Ā Ā Ā Ā Ā Ā Ā let speed = mid * A[i] + H[i];Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the bike is consideredĀ Ā Ā Ā Ā Ā Ā Ā // to be fast add it in sumĀ Ā Ā Ā Ā Ā Ā Ā if (speed >= L) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum += speed;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return the resultant sumĀ Ā Ā Ā return sum;}Ā
// Function to find the minimum number// of time requiredfunction buzzTime(N, M, L, H, A){Ā
Ā Ā Ā Ā // Stores the range of Binary SearchĀ Ā Ā Ā let low = 0, high = 1e10;Ā
Ā Ā Ā Ā // Stores the minimum number ofĀ Ā Ā Ā // time requiredĀ Ā Ā Ā let ans = 0;Ā
Ā Ā Ā Ā while (high >= low) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Find the value of midĀ Ā Ā Ā Ā Ā Ā Ā let mid = Math.floor(low + (high - low) / 2);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the mid is the resultantĀ Ā Ā Ā Ā Ā Ā Ā // speed requiredĀ Ā Ā Ā Ā Ā Ā Ā if (check(H, A, mid, N, M, L)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā >= M) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the ans and highĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = mid;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā high = mid - 1;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // OtherwiseĀ Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā low = mid + 1;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return the minimum number of hoursĀ Ā Ā Ā return ans;}Ā
// Driver CodeĀ Ā Ā Ā 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));Ā Ā Ā Ā Ā Ā Ā Ā Ā // This code is contributed by _saurabh_jaiswal.</script> |
3
Ā
Time Complexity:O(N*log(max(L, M)))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Read More Information here on that Topic: geeksforgeeks.org/minimum-time-remaining-for-safety-alarm-to-start/ […]