# Calculate Delayed Arrival Time
**Difficulty:** EASY
[External](https://leetcode.com/problems/calculate-delayed-arrival-time)
Canonical: https://scaleengineer.com/dsa/problems/calculate-delayed-arrival-time
**Patterns:** [Math](https://scaleengineer.com/dsa/patterns/math)
---
## 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 < 24`
* `1 <= delayedTime <= 24`

# Approaches
## Iterative Simulation
This approach simulates the passage of time hour by hour. We start with the `arrivalTime` and increment it for each hour of the delay. We handle the 24-hour clock wrap-around by resetting the time to 0 whenever it reaches 24.
**Time:** O(D) - The time complexity is linear with respect to the `delayedTime` (D), because the loop runs once for each hour of delay. · **Space:** O(1) - Constant space is used, as we only need a few variables to store the times, regardless of the input size.
**Pros:** The logic is very straightforward and easy to understand, as it directly mimics a clock ticking forward.
**Cons:** This approach is computationally inefficient compared to a direct mathematical formula, especially if `delayedTime` were large.; The code is more verbose and complex than necessary for this problem.
### Explanation
The algorithm initializes a variable, say `currentTime`, with the given `arrivalTime`. It then enters a loop that iterates `delayedTime` times. In each iteration, `currentTime` is incremented by one. After each increment, we check if `currentTime` has reached 24. If it has, we reset it to 0, simulating the start of a new day. After the loop completes, `currentTime` will hold the final arrival time.

```java
class Solution {
    public int findDelayedArrivalTime(int arrivalTime, int delayedTime) {
        int currentTime = arrivalTime;
        for (int i = 0; i < delayedTime; i++) {
            currentTime++;
            if (currentTime == 24) {
                currentTime = 0;
            }
        }
        return currentTime;
    }
}
```
### Algorithm
- Initialize a variable `currentTime` with `arrivalTime`.
- Create a loop that iterates `delayedTime` times.
- Inside the loop, increment `currentTime` by 1.
- After each increment, check if `currentTime` is equal to 24. If it is, reset `currentTime` to 0.
- After the loop finishes, return the final `currentTime`.

## Direct Calculation with Modulo Operator
This is the most efficient and direct approach. The problem can be modeled using modular arithmetic. A 24-hour clock is a system of arithmetic modulo 24. The new arrival time is simply the sum of the initial arrival time and the delay, taken modulo 24.
**Time:** O(1) - The solution consists of a single addition and a single modulo operation, both of which are constant-time operations. · **Space:** O(1) - The calculation uses a constant amount of memory, regardless of the input values.
**Pros:** Extremely efficient, performing the calculation in constant time.; The code is concise, elegant, and directly reflects the mathematical nature of the problem.; It is a robust solution that correctly handles all edge cases within the problem's constraints.
**Cons:** Requires understanding of the modulo operator, which might be slightly less intuitive for absolute beginners compared to a simple loop.
### Explanation
The core idea is to recognize that time wraps around every 24 hours. The modulo operator (`%`) is the perfect tool for this kind of cyclical behavior. First, we calculate the total time by adding `arrivalTime` and `delayedTime`. Then, we apply the modulo operator with 24 to this sum. The expression `(arrivalTime + delayedTime) % 24` gives the remainder when the sum is divided by 24. This remainder is exactly the time on a 24-hour clock. For instance, a sum of 24 results in `24 % 24 = 0`, and a sum of 25 results in `25 % 24 = 1`, which correctly represents the time on the next day. This single line of calculation provides the correct result for all valid inputs.

```java
class Solution {
    public int findDelayedArrivalTime(int arrivalTime, int delayedTime) {
        return (arrivalTime + delayedTime) % 24;
    }
}
```
### Algorithm
- Add the `arrivalTime` and `delayedTime` together to get a `totalTime`.
- Use the modulo operator (`%`) to find the remainder of `totalTime` when divided by 24.
- Return the result of this operation.

# Solutions
### Java

```java
class Solution {
public
  int findDelayedArrivalTime(int arrivalTime, int delayedTime) {
    return (arrivalTime + delayedTime) % 24;
  }
}

```

### CPP

```cpp
class Solution {
public:
  int findDelayedArrivalTime(int arrivalTime, int delayedTime) {
    return (arrivalTime + delayedTime) % 24;
  }
};

```

### Python

```python
class Solution:
    def findDelayedArrivalTime(
        self, arrivalTime: int, delayedTime: int) -> int: return (arrivalTime + delayedTime) % 24

```
