Open In App

Minimum time remaining for safety alarm to start

Last Updated : 16 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 condition
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[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 required
long 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 Code
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




// Java program for the above approach
import 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 condition
def 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 required
def 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 Code
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))
     
    # This code is contributed by ipg2016107.


C#




// C# program for the above approach
using System;
 
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;
     
    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 condition
function 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 required
function 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>


Output: 

3

 

Time Complexity:O(N*log(max(L, M)))
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads