Minimum Time to Trigger Alarm

Last Updated : 23 Jul, 2025

A bike race is being organized with n bikers. The initial speed and the acceleration of each biker are provided in two arrays: a[] (initial speed) and b[] (acceleration). A biker is considered fast if their speed at any given hour is greater than or equal to a threshold value s. The total speed on the track for each hour is calculated by summing the speeds of all fast bikers at that hour. Once the total speed on the track reaches or exceeds a threshold value m, the safety alarm turns on. The task is to find the minimum number of hours after which the safety alarm will start.

Hint : We can compute the current speed using basic acceleration and speed formula and assuming uniform acceleration.
current speed = initial speed + acceleration * time

Examples:

Input: n = 3, m = 400, s = 120, a[] = [20, 50, 20], b[] = [20, 70, 90]
Output: 3
Explanation: Speeds of all the Bikers after every hour starting from 0
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=550
The Alarm will start at 3rd Hour.

Input: n = 2, m = 60, s = 120, a[] = [50, 30], b[] = [20, 40]
Output: 3

Try It Yourself
redirect icon

[Naive Approach] Direct Calculation Method - O(n * max(s, m)) and O(1) space

The approach iterates through each hour, calculating the speed of every biker at that particular hour. For each biker, the speed is computed as the initial speed plus the product of their acceleration and the hour. If a biker's speed meets or exceeds the threshold s, they are considered "fast" and their speed is added to the total speed on the track. The loop continues until the total speed reaches or exceeds the threshold m, at which point the function returns the number of hours required to activate the safety alarm.

C++
#include <iostream>
#include <vector>
using namespace std;

int buzzTime(int n, int m, int s, vector<int>& a, vector<int>& b) {
    int hour = 0;
    while (true) {
        int totalSpeed = 0;
        for (int i = 0; i < n; ++i) {
            int currSpeed = a[i] + b[i] * hour;
            if (currSpeed >= s) {
                totalSpeed += currSpeed ;
            }
        }
        if (totalSpeed >= m) {
            return hour;
        }
        hour++;
    }
}

int main() {
    int n = 3;
    int m = 400;   
    int s = 120;  
    vector<int> a = {20, 50, 20};  
    vector<int> b = {20, 70, 90};  

    cout << buzzTime(n, m, s, a, b) << endl;
    
    return 0;
}
Java
import java.util.*;

public class GfG {
    public static int buzzTime(int n, int m, int s, int[] a, int[] b) {
        int hour = 0;
        while (true) {
            int totalSpeed = 0;
            for (int i = 0; i < n; ++i) {
                int currSpeed = a[i] + b[i] * hour;
                if (currSpeed >= s) {
                    totalSpeed += currSpeed;
                }
            }
            if (totalSpeed >= m) {
                return hour;
            }
            hour++;
        }
    }

    public static void main(String[] args) {
        int n = 3;
        int m = 400;
        int s = 120;
        int[] a = {20, 50, 20};
        int[] b = {20, 70, 90};

        System.out.println(buzzTime(n, m, s, a, b));
    }
}
Python
def buzzTime(n, m, s, a, b):
    hour = 0
    while True:
        totalSpeed = 0
        for i in range(n):
            currSpeed = a[i] + b[i] * hour
            if currSpeed >= s:
                totalSpeed += currSpeed
        if totalSpeed >= m:
            return hour
        hour += 1

if __name__ == '__main__':
    n = 3
    m = 400
    s = 120
    a = [20, 50, 20]
    b = [20, 70, 90]

    print(buzzTime(n, m, s, a, b))
C#
using System;
using System.Linq;

class GfG {
    public static int buzzTime(int n, int m, int s, int[] a, int[] b) {
        int hour = 0;
        while (true) {
            int totalSpeed = 0;
            for (int i = 0; i < n; ++i) {
                int currSpeed = a[i] + b[i] * hour;
                if (currSpeed >= s) {
                    totalSpeed += currSpeed;
                }
            }
            if (totalSpeed >= m) {
                return hour;
            }
            hour++;
        }
    }

    static void Main() {
        int n = 3;
        int m = 400;
        int s = 120;
        int[] a = {20, 50, 20};
        int[] b = {20, 70, 90};

        Console.WriteLine(buzzTime(n, m, s, a, b));
    }
}
JavaScript
function buzzTime(n, m, s, a, b) {
    let hour = 0;
    while (true) {
        let totalSpeed = 0;
        for (let i = 0; i < n; i++) {
            let currSpeed = a[i] + b[i] * hour;
            if (currSpeed >= s) {
                totalSpeed += currSpeed;
            }
        }
        if (totalSpeed >= m) {
            return hour;
        }
        hour++;
    }
}

const n = 3;
const m = 400;
const s = 120;
const a = [20, 50, 20];
const b = [20, 70, 90];

console.log(buzzTime(n, m, s, a, b));

Output
3

[Expected Approach] Using Binary Search - O(n * log(max(s, m))) and O(1) space

The problem can be solved using Binary Search based on the observation that if the total speed on the track reaches or exceeds M at hour i, it will continue to do so at hour i+1 due to the increasing speeds of the bikers.

Steps:

  1. Set the minimum time as 0 and the maximum time as max(s, m).
  2. Perform Binary Search over the time range:
    • For each midpoint hour, calculate the total speed by checking each biker's speed.
    • If the total speed is at least m, reduce the search range to the left; otherwise, increase the range to the right.
  3. Return the minimum hour when the total speed exceeds or equals m.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int buzzTime(int n, int m, int s, vector<int>& a, vector<int>& b)
{
    int l = 0, r = 0, mid, sum = 0;
    int x = max(m, s);

    // Loop to find the maximum possible time
    for (int i = 0; i < n; i++) {
        if ((x - a[i]) % b[i] == 0)
            r = max(r, (x - a[i]) / b[i]);
        else
            r = max(r, (x - a[i]) / b[i] + 1);
    }

    // Loop to implement binary search
    while (l <= r) {
        mid = (l + r) / 2;
        sum = 0;

        // Loop to calculate the speed of
        // each bike at that time
        for (int i = 0; i < n; i++)
            if ((a[i] + b[i] * mid) >= s)
                sum += (a[i] + b[i] * mid);

        // Check if the speed of the track
        // is at least m
        if (sum >= m)
            r = mid - 1;
        else
            l = mid + 1;
    }

    // Return the minimum time required
    return l;
}

int main()
{
    int s = 120, m = 400;
    vector<int> a = { 20, 50, 20 };
    vector<int> b = { 20, 70, 90 };
    int n = a.size();

    int minHour = buzzTime(n, m, s, a, b);
    cout << minHour;
    return 0;
}
Java
import java.util.*;

public class GfG {
    public static int buzzTime(int n, int m, int s, int[] a, int[] b) {
        int l = 0, r = 0, mid, sum = 0;
        int x = Math.max(m, s);

        // Loop to find the maximum possible time
        for (int i = 0; i < n; i++) {
            if ((x - a[i]) % b[i] == 0)
                r = Math.max(r, (x - a[i]) / b[i]);
            else
                r = Math.max(r, (x - a[i]) / b[i] + 1);
        }

        // Loop to implement binary search
        while (l <= r) {
            mid = (l + r) / 2;
            sum = 0;

            // Loop to calculate the speed of
            // each bike at that time
            for (int i = 0; i < n; i++)
                if ((a[i] + b[i] * mid) >= s)
                    sum += (a[i] + b[i] * mid);

            // Check if the speed of the track
            // is at least m
            if (sum >= m)
                r = mid - 1;
            else
                l = mid + 1;
        }

        // Return the minimum time required
        return l;
    }

    public static void main(String[] args) {
        int s = 120, m = 400;
        int[] a = { 20, 50, 20 };
        int[] b = { 20, 70, 90 };
        int n = a.length;

        int minHour = buzzTime(n, m, s, a, b);
        System.out.println(minHour);
    }
}
Python
def buzzTime(n, m, s, a, b):
    l, r, sum = 0, 0, 0
    x = max(m, s)

    # Loop to find the maximum possible time
    for i in range(n):
        if (x - a[i]) % b[i] == 0:
            r = max(r, (x - a[i]) // b[i])
        else:
            r = max(r, (x - a[i]) // b[i] + 1)

    # Loop to implement binary search
    while l <= r:
        mid = (l + r) // 2
        sum = 0

        # Loop to calculate the speed of
        # each bike at that time
        for i in range(n):
            if (a[i] + b[i] * mid) >= s:
                sum += (a[i] + b[i] * mid)

        # Check if the speed of the track
        # is at least m
        if sum >= m:
            r = mid - 1
        else:
            l = mid + 1

    # Return the minimum time required
    return l

s = 120
m = 400
a = [20, 50, 20]
b = [20, 70, 90]
n = len(a)

minHour = buzzTime(n, m, s, a, b)
print(minHour)
C#
using System;
using System.Linq;

class GfG {
    public static int BuzzTime(int n, int m, int s, int[] a, int[] b) {
        int l = 0, r = 0, mid, sum = 0;
        int x = Math.Max(m, s);

        // Loop to find the maximum possible time
        for (int i = 0; i < n; i++) {
            if ((x - a[i]) % b[i] == 0)
                r = Math.Max(r, (x - a[i]) / b[i]);
            else
                r = Math.Max(r, (x - a[i]) / b[i] + 1);
        }

        // Loop to implement binary search
        while (l <= r) {
            mid = (l + r) / 2;
            sum = 0;

            // Loop to calculate the speed of
            // each bike at that time
            for (int i = 0; i < n; i++)
                if ((a[i] + b[i] * mid) >= s)
                    sum += (a[i] + b[i] * mid);

            // Check if the speed of the track
            // is at least m
            if (sum >= m)
                r = mid - 1;
            else
                l = mid + 1;
        }

        // Return the minimum time required
        return l;
    }

    static void Main() {
        int s = 120, m = 400;
        int[] a = { 20, 50, 20 };
        int[] b = { 20, 70, 90 };
        int n = a.Length;

        int minHour = BuzzTime(n, m, s, a, b);
        Console.WriteLine(minHour);
    }
}
JavaScript
function buzzTime(n, m, s, a, b) {
    let l = 0, r = 0, mid, sum = 0;
    let x = Math.max(m, s);

    // Loop to find the maximum possible time
    for (let i = 0; i < n; i++) {
        if ((x - a[i]) % b[i] === 0)
            r = Math.max(r, (x - a[i]) / b[i]);
        else
            r = Math.max(r, Math.floor((x - a[i]) / b[i]) + 1);
    }

    // Loop to implement binary search
    while (l <= r) {
        mid = Math.floor((l + r) / 2);
        sum = 0;

        // Loop to calculate the speed of
        // each bike at that time
        for (let i = 0; i < n; i++)
            if ((a[i] + b[i] * mid) >= s)
                sum += (a[i] + b[i] * mid);

        // Check if the speed of the track
        // is at least m
        if (sum >= m)
            r = mid - 1;
        else
            l = mid + 1;
    }

    // Return the minimum time required
    return l;
}

let s = 120, m = 400;
let a = [20, 50, 20];
let b = [20, 70, 90];
let n = a.length;

let minHour = buzzTime(n, m, s, a, b);
console.log(minHour);

Output
3
Comment