VOOZH about

URL: https://www.geeksforgeeks.org/dsa/waiting-time-of-the-last-consumer/

⇱ Waiting Time of the Last Consumer - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Waiting Time of the Last Consumer

Last Updated : 7 Jun, 2026

Given two positive integers n and x,  n denotes the total number of consumers, x denotes the time interval, after which a new consumer arrives. A single producer serves one consumer at a time, and each consumer requires exactly 10 minutes of service time. Determine the waiting time for the last consumer before service begins.

Examples:

Input: n = 4, x = 5
Output: 15
Explanation: The arrival time of the patients are 0, 5, 10 and 15 respectively. The times when the first three patients are checked are respectively 10, 20, 30. So the Waiting time is 30 - 15 = 15.

Input: n = 3, x = 10
Output: 0
Explanation: The incoming time of the patients are 0, 10 and 20 and the first two patients are finished checking at 10, 20. So the last patient does not need to wait.

[Naive Approach] Simulate Every Consumer - O(n) Time O(1) Space

The idea is to simulate the service process consumer by consumer. For each consumer, calculate their arrival time, determine when service can start, and update the producer's next free time. The waiting time of the last consumer is then returned.


Output
15

Time Complexity: O(n)
Auxiliary Space: O(1)

[Expected Approach] Mathematical Observation - O(1) Time O(1) Space

The idea is to observe that each consumer requires 10 minutes of service. If consumers arrive every x minutes and x >= 10, the producer finishes serving one consumer before the next arrives, so no waiting occurs. Otherwise, each new consumer adds (10 - x) minutes to the queue, and this delay accumulates for all (n - 1) consumers before the last consumer, allowing the waiting time to be calculated directly using a formula.


Output
15

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: