Calculate Delayed Arrival Time solution and explanation
9 more videos on this problem
Leetcode | 2651. Calculate Delayed Arrival Time | Easy | Java SolutionDeveloper Docs · 3:59 · 672 views
6387. Calculate Delayed Arrival Time | Leetcode Weekly Contest 342 | Solution In Comment | JAVA.Optimization · 2:42 · 405 views
Leetcode Weekly Contest 342 | PROBLEM 2651, 2652, 2653, 2654code Explainer · 21:09 · 336 views
Leetcode 2651. Calculate Delayed Arrival Time | Weekly Contest 342 | Leetcode weekly contest 😇😇OffCampus Phodenge | Aman Chowdhury · 4:54 · 200 views
2651. Calculate Delayed Arrival Time (Leetcode Easy)Programming Live with Larry · 2:04 · 161 views
Leetcode Weekly contest 342 - Easy - Calculate Delayed Arrival TimePrakhar Agrawal · 2:00 · 145 views
Calculate Delayed Arrival Time - Leetcode 2651 - Python | Weekly 342Algorithms Casts · 2:07 · 112 views
LeetCode Weekly contest 342 || 2651. Calculate Delayed Arrival Time || 2652. Sum MultiplesAshish Kumar · 2:11 · 97 views
Calculate Delayed Arrival Time | Leetcode 2651| Approach + Code with explanation.Pirai's Academy · 3:28 · 36 views
Leetcode 2651. Calculate Delayed Arrival Time || Contest 342ExpertFunda · 4:33 · 16 views
The 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 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 <= 24
Approach overview
Problem 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 1: Simple Addition and Modulus
This approach involves using simple arithmetic to add the delayed time to the arrival time and then using the modulus operator to handle the wrap-around effect that occurs after 23:00 on a 24-hour clock. By taking modulus of the sum with 24, you ensure that the resulting time stays within the 0 to 23 range, effectively resetting to 0 after 23.
def delayed_arrival_time(arrival_time, delayed_time):
return (arrival_time + delayed_time) % 24
print(delayed_arrival_time(15, 5)) # Output: 20
print(delayed_arrival_time(13, 11)) # Output: 0public class TrainArrival {
public static int delayedArrivalTime(int arrivalTime, int delayedTime) {
return (arrivalTime + delayedTime) % 24;
}
public static void main(String[] args) {
System.out.println(delayedArrivalTime(15, 5)); // Output: 20
System.out.println(delayedArrivalTime(13, 11)); // Output: 0
}
}#include <iostream>
int delayedArrivalTime(int arrivalTime, int delayedTime) {
return (arrivalTime + delayedTime) % 24;
}
int main() {
std::cout << delayedArrivalTime(15, 5) << std::endl; // Output: 20
std::cout << delayedArrivalTime(13, 11) << std::endl; // Output: 0
return 0;
}function delayedArrivalTime(arrivalTime, delayedTime) {
return (arrivalTime + delayedTime) % 24;
}
console.log(delayedArrivalTime(15, 5)); // Output: 20
console.log(delayedArrivalTime(13, 11)); // Output: 0#include <stdio.h>
int delayedArrivalTime(int arrivalTime, int delayedTime) {
return (arrivalTime + delayedTime) % 24;
}
int main() {
printf("%d\n", delayedArrivalTime(15, 5)); // Output: 20
printf("%d\n", delayedArrivalTime(13, 11)); // Output: 0
return 0;
}using System;
class Program {
static int DelayedArrivalTime(int arrivalTime, int delayedTime) {
return (arrivalTime + delayedTime) % 24;
}
static void Main() {
Console.WriteLine(DelayedArrivalTime(15, 5)); // Output: 20
Console.WriteLine(DelayedArrivalTime(13, 11)); // Output: 0
}
}The Python solution uses a function delayed_arrival_time which returns the new time by adding arrival and delayed times and using the modulus operator to ensure the result fits in a 24-hour format.
Time Complexity: O(1) as it's a straightforward mathematical calculation.
Space Complexity: O(1) because no additional space is allocated.
Approach 2: Alternative Calculation using Conditional Logic
Instead of using the modulus operator directly, this approach utilizes conditional logic to determine if the calculated time exceeds 23:00. If it does, the result is adjusted by subtracting 24, effectively wrapping it back to start from 0.
def delayed_arrival_time_alt(arrival_time, delayed_time):
new_time = arrival_time + delayed_time
return new_time - 24 if new_time >= 24 else new_time
print(delayed_arrival_time_alt(15, 5)) # Output: 20
print(delayed_arrival_time_alt(13, 11)) # Output: 0public class TrainArrivalAlt {
public static int delayedArrivalTimeAlt(int arrivalTime, int delayedTime) {
int newTime = arrivalTime + delayedTime;
return newTime >= 24 ? newTime - 24 : newTime;
}
public static void main(String[] args) {
System.out.println(delayedArrivalTimeAlt(15, 5)); // Output: 20
System.out.println(delayedArrivalTimeAlt(13, 11)); // Output: 0
}
}#include <iostream>
int delayedArrivalTimeAlt(int arrivalTime, int delayedTime) {
int newTime = arrivalTime + delayedTime;
return newTime >= 24 ? newTime - 24 : newTime;
}
int main() {
std::cout << delayedArrivalTimeAlt(15, 5) << std::endl; // Output: 20
std::cout << delayedArrivalTimeAlt(13, 11) << std::endl; // Output: 0
return 0;
}function delayedArrivalTimeAlt(arrivalTime, delayedTime) {
const newTime = arrivalTime + delayedTime;
return newTime >= 24 ? newTime - 24 : newTime;
}
console.log(delayedArrivalTimeAlt(15, 5)); // Output: 20
console.log(delayedArrivalTimeAlt(13, 11)); // Output: 0#include <stdio.h>
int delayedArrivalTimeAlt(int arrivalTime, int delayedTime) {
int newTime = arrivalTime + delayedTime;
return newTime >= 24 ? newTime - 24 : newTime;
}
int main() {
printf("%d\n", delayedArrivalTimeAlt(15, 5)); // Output: 20
printf("%d\n", delayedArrivalTimeAlt(13, 11)); // Output: 0
return 0;
}using System;
class Program {
static int DelayedArrivalTimeAlt(int arrivalTime, int delayedTime) {
int newTime = arrivalTime + delayedTime;
return newTime >= 24 ? newTime - 24 : newTime;
}
static void Main() {
Console.WriteLine(DelayedArrivalTimeAlt(15, 5)); // Output: 20
Console.WriteLine(DelayedArrivalTimeAlt(13, 11)); // Output: 0
}
}Similarly, in Python, the function delayed_arrival_time_alt deploys a conditional logic to handle situations where the resultant time exceeds a day, subtracting 24 hours as needed.
Time Complexity: O(1) due to straightforward calculations.
Space Complexity: O(1) because it utilizes a constant amount of space.
Approach 3: Default Approach
function findDelayedArrivalTime(arrivalTime: number, delayedTime: number): number {
return (arrivalTime + delayedTime) % 24;
}func findDelayedArrivalTime(arrivalTime int, delayedTime int) int {
return (arrivalTime + delayedTime) % 24
}impl Solution {
pub fn find_delayed_arrival_time(arrival_time: i32, delayed_time: i32) -> i32 {
(arrival_time + delayed_time) % 24
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Simple Addition and Modulus | Time Complexity: O(1) as it's a straightforward mathematical calculation. |
| Alternative Calculation using Conditional Logic | Time Complexity: O(1) due to straightforward calculations. |
| Default Approach | – |
Complexity analysis
| 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 |
Frequently asked questions
Is Calculate Delayed Arrival Time easy or hard?
Calculate Delayed Arrival Time is classified as an Easy problem. It tests understanding of basic arithmetic and modulo operations rather than complex algorithms or data structures.
Calculate Delayed Arrival Time Python/Java solution
In Python or Java, compute the result using (arrivalTime + delayedTime) % 24. The same logic works across C++, JavaScript, C#, and other languages because modulo arithmetic behaves consistently for this case.
How to solve Calculate Delayed Arrival Time in O(1)?
Add the delay to the arrival time and apply modulo 24 to keep the result within the 24‑hour clock range. The formula (arrivalTime + delayedTime) % 24 immediately returns the correct hour after wraparound.
What is the best approach for Calculate Delayed Arrival Time?
The best approach uses simple addition followed by modulo 24: (arrivalTime + delayedTime) % 24. This directly models the wraparound behavior of a 24‑hour clock and runs in O(1) time with O(1) space.
Is Calculate Delayed Arrival Time asked at Google/Amazon/Meta?
Problems like this appear in coding practice sets and entry‑level interview preparation. While the exact question is unlikely at major companies, the concept of modular arithmetic frequently appears in scheduling, circular arrays, and time calculations.
What data structure is used in Calculate Delayed Arrival Time?
No special data structure is required. The solution relies purely on arithmetic operations and modular math to simulate a circular 24‑hour clock.
What is the time complexity of Calculate Delayed Arrival Time?
The problem has constant time complexity O(1) because it requires only a single arithmetic calculation. Space complexity is also O(1) since no additional data structures are used.
