Calculate Delayed Arrival Time - Problem

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 actually arrive at the station.

Note that the time in this problem is in 24-hours format.

Input & Output

Example 1 — Basic Case
$ Input: arrivalTime = 15, delayedTime = 5
Output: 20
💡 Note: Train was scheduled at 15:00, delayed by 5 hours, so arrives at 20:00
Example 2 — Wraparound Case
$ Input: arrivalTime = 22, delayedTime = 5
Output: 3
💡 Note: 22 + 5 = 27, but 27 % 24 = 3, so train arrives at 03:00 next day
Example 3 — Large Delay
$ Input: arrivalTime = 10, delayedTime = 50
Output: 12
💡 Note: 10 + 50 = 60, and 60 % 24 = 12, so arrives at 12:00 after multiple day cycles

Constraints

  • 1 ≤ arrivalTime ≤ 23
  • 1 ≤ delayedTime ≤ 109

Visualization

Tap to expand
Calculate Delayed Arrival Time INPUT 12 3 6 9 15:00 arrivalTime 15 delayedTime 5 ALGORITHM STEPS 1 Get Inputs arrival=15, delay=5 2 Add Times sum = 15 + 5 = 20 3 Apply Modulo 24 result = 20 % 24 4 Return Result return 20 Formula: (arrival + delay) % 24 (15 + 5) % 24 = 20 FINAL RESULT 12 3 6 9 20:00 Output: 20 OK - Valid 24h time Key Insight: The modulo 24 operation handles the wrap-around at midnight. For example, if arrival=23 and delay=5, then (23+5)%24 = 28%24 = 4 (4:00 AM next day). When sum is less than 24, modulo returns same value. TutorialsPoint - Calculate Delayed Arrival Time | Optimal Solution
Asked in
Meta 15 Google 12
16.7K Views
Medium Frequency
~5 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen