Calculate Delayed Arrival Time
We can find the delayed arrival time by adding the arrival time and the delayed time together. Since the time is in 24-hour format, if the result exceeds or equals 24, we need to subtract 24 from it to get the equivalent time on the next day.
Here’s a step-by-step explanation:
- Add Arrival and Delayed Time: Sum the
arrivalTime
anddelayedTime
. - Adjust for 24-hour Format: If the sum is 24 or more, subtract 24 to convert to 24-hour format.
Python solution:
|
|
Explanation:
- If the arrival time is 15 and the delay is 5, then 15 + 5 = 20, so the train arrives at 20:00.
- If the arrival time is 13 and the delay is 11, then 13 + 11 = 24, but in 24-hour format, we represent this as 00:00, so the function returns 0.
The code handles both cases and returns the correct arrival time in 24-hour format. Its time complexity is O(1).