We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Calculate Delayed Arrival Time

Number: 2748

Difficulty: Easy

Paid? No

Companies: N/A


Problem Description

Given a train's arrival time and the delay in hours, compute the new arrival time in 24-hour format. If the sum equals 24, the time wraps around to 0 (midnight).


Key Insights

  • The problem requires a simple addition of two values: arrivalTime and delayedTime.
  • Since time is in 24-hour format, the result must be taken modulo 24.
  • Using the modulo operator correctly handles any wrap-around cases (e.g., when the result is 24 or greater).

Space and Time Complexity

Time Complexity: O(1) Space Complexity: O(1)


Solution

The solution is straightforward:

  1. Add the arrivalTime and delayedTime.
  2. Use the modulo operator with 24 to adjust the result to the 24-hour format.
  3. Return the computed time. This approach efficiently uses constant time and space, relying only on basic arithmetic operations.

Code Solutions

# Compute the delayed arrival time
def calculate_delayed_arrival(arrivalTime, delayedTime):
    # Add the two times and use modulo 24 to wrap around if necessary
    arrival_with_delay = (arrivalTime + delayedTime) % 24
    return arrival_with_delay

# Example usage
if __name__ == "__main__":
    # Example 1
    print(calculate_delayed_arrival(15, 5))  # Expected output: 20
    # Example 2
    print(calculate_delayed_arrival(13, 11)) # Expected output: 0
← Back to All Questions