Watch 10 video solutions for Calculate Delayed Arrival Time, a easy level problem involving Math. This walkthrough by CodeJulian has 1,388 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a positive integer arrivalTime denoting the arrival time of a train in hours, and another positive integer delayedTime denoting the amount of delay in hours.
Return the time when the train will arrive at the station.
Note that the time in this problem is in 24-hours format.
Example 1:
Input: arrivalTime = 15, delayedTime = 5 Output: 20 Explanation: Arrival time of the train was 15:00 hours. It is delayed by 5 hours. Now it will reach at 15+5 = 20 (20:00 hours).
Example 2:
Input: arrivalTime = 13, delayedTime = 11 Output: 0 Explanation: Arrival time of the train was 13:00 hours. It is delayed by 11 hours. Now it will reach at 13+11=24 (Which is denoted by 00:00 in 24 hours format so return 0).
Constraints:
1 <= arrivaltime < 241 <= delayedTime <= 24Problem Overview: You are given an arrivalTime (0–23) and a delayedTime. The task is to compute the new arrival hour after the delay while keeping the result within a 24‑hour clock. If the sum exceeds 23, the clock wraps around to the beginning.
Approach 1: Simple Addition and Modulus (O(1) time, O(1) space)
The most direct solution adds the delay to the original arrival hour and applies modulo 24. The modulo operation automatically wraps values back into the valid range of a 24‑hour clock. The calculation is (arrivalTime + delayedTime) % 24. This works because modular arithmetic models cyclic systems like clocks. The approach uses constant time and space since it performs only a single arithmetic operation and returns the result.
This technique relies on basic math and modular arithmetic. Whenever a problem involves circular ranges (hours, days of week, rotating indices), modulo arithmetic provides a clean and reliable solution.
Approach 2: Alternative Calculation using Conditional Logic (O(1) time, O(1) space)
You can also compute the result using conditional checks instead of modulo. First calculate sum = arrivalTime + delayedTime. If the sum is less than 24, it is already a valid hour. If it is 24 or greater, subtract 24 to wrap the value back into the correct range. This mirrors how a physical clock resets after midnight.
This method uses straightforward arithmetic and conditional branching. It may feel more intuitive if you are thinking about the real-world behavior of clocks rather than mathematical cycles. However, the modulo version is shorter and scales better when working with other cyclic problems.
Recommended for interviews: Interviewers generally expect the modulo solution because it demonstrates comfort with cyclic calculations and produces the cleanest code. The conditional version shows you understand the underlying behavior, but the expression (arrivalTime + delayedTime) % 24 communicates the intent immediately and avoids extra branching.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simple Addition and Modulus | O(1) | O(1) | Best general solution for clock-style wraparound problems |
| Conditional Logic with Wraparound | O(1) | O(1) | Useful when modulo is avoided or when explaining the clock behavior step by step |