Minimum Time to Trigger Alarm

Last Updated : 2 Sep, 2026

Given n bikers, the i-th biker has an initial speed h[i] and acceleration a[i].

  • At hour t, the speed of the i-th biker is: h[i] + a[i] × t
  • A biker is considered fast if their speed is at least l.
  • The alarm turns on when the sum of the speeds of all fast bikers is at least m.

Find the minimum hour at which the alarm turns on.

Examples:

Input: n = 3, m = 400, l = 120, h[] = [20, 50, 20], a[] = [20, 70, 90]
Output: 3
Explanation: At hour 3, the speeds are 80, 260, 290. The second and third bikers are fast because their speeds are at least 120. Their total speed is 260 + 290 = 550, which is at least 400. Therefore, the minimum hour is 3.

Input: n = 2, m = 60, l = 120, h[] = [50, 30], a[] = [20, 40]
Output: 3
Explanation: At hour 3, the speeds are 110 and 150. Only the second biker is fast, and the total speed of fast bikers is 150, which is at least 60. Therefore, the minimum hour is 3.

Try It Yourself
redirect icon

[Naive Approach] Linear Search - O(n*max(m, l)) Time and O(1) Auxiliary Space

The idea is to check each hour one by one starting from 0 until the alarm turns on.

  • For every hour, calculate the speed of each biker using: speed = h[i] + a[i] * hour
  • If a biker's speed is at least l, add it to the total speed.
  • If the total speed becomes at least m, the alarm turns on, so return the current hour.

Since we check every hour sequentially, the first hour satisfying the condition is guaranteed to be the minimum hour.

C++
#include <bits/stdc++.h>
using namespace std;

int buzzTime(int n, int m, int l, vector<int>& h, vector<int>& a) {
    int hour = 0;

    while (true) {
        long long sum = 0;

        // Calculate the total speed of fast bikers
        for (int i = 0; i < n; i++) {
            long long speed = h[i] + (long long)a[i] * hour;

            if (speed >= l)
                sum += speed;
        }

        // Check if the alarm turns on
        if (sum >= m)
            return hour;

        hour++;
    }
}

int main() {
    int n = 3;
    int m = 400;
    int l = 120;

    vector<int> h = {20, 50, 20};
    vector<int> a = {20, 70, 90};

    cout << buzzTime(n, m, l, h, a) << endl;

    return 0;
}
Java
class GFG {

    static int buzzTime(int n, int m, int l, int[] h, int[] a) {
        int hour = 0;

        while (true) {
            long sum = 0;

            // Calculate the total speed of fast bikers
            for (int i = 0; i < n; i++) {
                long speed = h[i] + (long) a[i] * hour;

                if (speed >= l)
                    sum += speed;
            }

            // Check if the alarm turns on
            if (sum >= m)
                return hour;

            hour++;
        }
    }

    public static void main(String[] args) {
        int n = 3;
        int m = 400;
        int l = 120;

        int[] h = {20, 50, 20};
        int[] a = {20, 70, 90};

        System.out.println(buzzTime(n, m, l, h, a));
    }
}
Python
def buzzTime(n, m, l, h, a):
    hour = 0

    while True:
        sum = 0

        # Calculate the total speed of fast bikers
        for i in range(n):
            speed = h[i] + a[i] * hour

            if speed >= l:
                sum += speed

        # Check if the alarm turns on
        if sum >= m:
            return hour

        hour += 1


if __name__ == "__main__":
    n = 3
    m = 400
    l = 120

    h = [20, 50, 20]
    a = [20, 70, 90]

    print(buzzTime(n, m, l, h, a))
C#
using System;

class GFG
{
    static int buzzTime(int n, int m, int l, int[] h, int[] a)
    {
        int hour = 0;

        while (true)
        {
            long sum = 0;

            // Calculate the total speed of fast bikers
            for (int i = 0; i < n; i++)
            {
                long speed = h[i] + (long)a[i] * hour;

                if (speed >= l)
                    sum += speed;
            }

            // Check if the alarm turns on
            if (sum >= m)
                return hour;

            hour++;
        }
    }

    static void Main()
    {
        int n = 3;
        int m = 400;
        int l = 120;

        int[] h = { 20, 50, 20 };
        int[] a = { 20, 70, 90 };

        Console.WriteLine(buzzTime(n, m, l, h, a));
    }
}
JavaScript
function buzzTime(n, m, l, h, a) {
    let hour = 0;

    while (true) {
        let sum = 0;

        // Calculate the total speed of fast bikers
        for (let i = 0; i < n; i++) {
            let speed = h[i] + a[i] * hour;

            if (speed >= l)
                sum += speed;
        }

        // Check if the alarm turns on
        if (sum >= m)
            return hour;

        hour++;
    }
}

// Driver code
let n = 3;
let m = 400;
let l = 120;

let h = [20, 50, 20];
let a = [20, 70, 90];

console.log(buzzTime(n, m, l, h, a));

Output
3

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

The idea is to use binary search to find the first hour when the alarm turns on.

At hour t, the speed of the i-th biker is: speed = h[i] + a[i] * t

A biker is considered fast if their speed is at least l. We add the speeds of all fast bikers and check whether their total speed is at least m.

As the hour increases, every biker's speed also increases. Therefore, the total speed of fast bikers never decreases. Once the alarm turns on, it remains on for all later hours. Hence, we can use binary search to find the first valid hour.

  • First, set low = 0 because the alarm may already turn on at hour 0.
  • To find a suitable upper bound, set: x = max(m, l)
  • For each biker, find the minimum hour when their speed becomes at least x: h[i] + a[i] * t >= x
  • This gives: t >= (x - h[i]) / a[i]
  • Since t must be an integer, take the ceiling of this value. If h[i] >= x, the required time is 0.
  • Calculate this time for every biker and take the maximum as high.
  • This guarantees that the answer lies in the range [0, high].

Now perform binary search on this range

C++
#include <bits/stdc++.h>
using namespace std;

int buzzTime(int n, int m, int l, vector<int>& h, vector<int>& a) {
    int low = 0, high = 0;
    int x = max(m, l);

    // Find an upper bound for the required hour
    for (int i = 0; i < n; i++) {
        if (x > h[i]) {
            int time = (x - h[i] + a[i] - 1) / a[i];
            high = max(high, time);
        }
    }

    // Binary search for the minimum hour
    while (low <= high) {
        int mid = low + (high - low) / 2;
        long long sum = 0;

        // Calculate the total speed of fast bikers
        for (int i = 0; i < n; i++) {
            long long speed = (long long)h[i] + (long long)a[i] * mid;

            if (speed >= l)
                sum += speed;
        }

        // Check if the alarm turns on
        if (sum >= m)
            high = mid - 1;
        else
            low = mid + 1;
    }

    return low;
}

int main() {
    int n = 3;
    int m = 400;
    int l = 120;

    vector<int> h = {20, 50, 20};
    vector<int> a = {20, 70, 90};

    cout << buzzTime(n, m, l, h, a) << endl;

    return 0;
}
Java
class GFG {

    static int buzzTime(int n, int m, int l, int[] h, int[] a) {
        int low = 0, high = 0;
        int x = Math.max(m, l);

        // Find an upper bound for the required hour
        for (int i = 0; i < n; i++) {
            if (x > h[i]) {
                int time = (x - h[i] + a[i] - 1) / a[i];
                high = Math.max(high, time);
            }
        }

        // Binary search for the minimum hour
        while (low <= high) {
            int mid = low + (high - low) / 2;
            long sum = 0;

            // Calculate the total speed of fast bikers
            for (int i = 0; i < n; i++) {
                long speed = (long) h[i] + (long) a[i] * mid;

                if (speed >= l)
                    sum += speed;
            }

            // Check if the alarm turns on
            if (sum >= m)
                high = mid - 1;
            else
                low = mid + 1;
        }

        return low;
    }

    public static void main(String[] args) {
        int n = 3;
        int m = 400;
        int l = 120;

        int[] h = {20, 50, 20};
        int[] a = {20, 70, 90};

        System.out.println(buzzTime(n, m, l, h, a));
    }
}
Python
def buzzTime(n, m, l, h, a):
    low = 0
    high = 0
    x = max(m, l)

    # Find an upper bound for the required hour
    for i in range(n):
        if x > h[i]:
            time = (x - h[i] + a[i] - 1) // a[i]
            high = max(high, time)

    # Binary search for the minimum hour
    while low <= high:
        mid = low + (high - low) // 2
        sum = 0

        # Calculate the total speed of fast bikers
        for i in range(n):
            speed = h[i] + a[i] * mid

            if speed >= l:
                sum += speed

        # Check if the alarm turns on
        if sum >= m:
            high = mid - 1
        else:
            low = mid + 1

    return low


if __name__ == "__main__":
    n = 3
    m = 400
    l = 120

    h = [20, 50, 20]
    a = [20, 70, 90]

    print(buzzTime(n, m, l, h, a))
C#
using System;

class GFG {

    static int buzzTime(int n, int m, int l, int[] h, int[] a) {
        int low = 0, high = 0;
        int x = Math.Max(m, l);

        // Find an upper bound for the required hour
        for (int i = 0; i < n; i++) {
            if (x > h[i]) {
                int time = (x - h[i] + a[i] - 1) / a[i];
                high = Math.Max(high, time);
            }
        }

        // Binary search for the minimum hour
        while (low <= high) {
            int mid = low + (high - low) / 2;
            long sum = 0;

            // Calculate the total speed of fast bikers
            for (int i = 0; i < n; i++) {
                long speed = (long)h[i] + (long)a[i] * mid;

                if (speed >= l)
                    sum += speed;
            }

            // Check if the alarm turns on
            if (sum >= m)
                high = mid - 1;
            else
                low = mid + 1;
        }

        return low;
    }

    static void Main() {
        int n = 3;
        int m = 400;
        int l = 120;

        int[] h = {20, 50, 20};
        int[] a = {20, 70, 90};

        Console.WriteLine(buzzTime(n, m, l, h, a));
    }
}
JavaScript
function buzzTime(n, m, l, h, a) {
    let low = 0, high = 0;
    let x = Math.max(m, l);

    // Find an upper bound for the required hour
    for (let i = 0; i < n; i++) {
        if (x > h[i]) {
            let time = Math.floor((x - h[i] + a[i] - 1) / a[i]);
            high = Math.max(high, time);
        }
    }

    // Binary search for the minimum hour
    while (low <= high) {
        let mid = low + Math.floor((high - low) / 2);
        let sum = 0;

        // Calculate the total speed of fast bikers
        for (let i = 0; i < n; i++) {
            let speed = h[i] + a[i] * mid;

            if (speed >= l)
                sum += speed;
        }

        // Check if the alarm turns on
        if (sum >= m)
            high = mid - 1;
        else
            low = mid + 1;
    }

    return low;
}

// Driver code
let n = 3;
let m = 400;
let l = 120;

let h = [20, 50, 20];
let a = [20, 70, 90];

console.log(buzzTime(n, m, l, h, a));

Output
3
Comment