cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2012 · EasyTime arithmetic

Travel Times

Autumn Tournament (2012)

Submission name: time.cpp

Problem summary

Three cars complete the same route. For each car, its departure and arrival clock times are given. Find the shortest and longest travel durations, accounting for a possible crossing of midnight.

Source clarification. The statement gives clock times but no day counters or duration bound. Following its midnight-crossing sample, durations are interpreted as less than one day; equal clock times produce 0:00. Longer journeys cannot be recovered from the supplied data.

Input

Three lines, each containing departure hour, departure minute, arrival hour and arrival minute.

Output

Minimum duration on the first line and maximum duration on the second. Print hours, a colon, and two minute digits; hours have no required leading zero, as in the sample.

Constraints

0 ≤ hours < 24; 0 ≤ minutes < 60.

Worked example

Original statement sample, translated without changing its numeric data.

Input

23 30 1 30
2 5 5 15
15 5 20 53

Output

2:00
5:48

Solution analysis

  1. Convert each clock reading to minutes after midnight. Subtract the departure from the arrival. If the result is negative, add 1440 minutes for the midnight crossing.
  2. Keep the minimum and maximum of the three durations. Convert each back using division and remainder by 60. Pad only the minute part to two digits.
  3. The sample durations are 120, 190 and 348 minutes. Their minimum is 2:00 and maximum is 5:48.

Why it works

For a same-day journey the clock difference equals elapsed minutes. For a journey crossing midnight, adding one day compensates for the clock reset. Minimum and maximum updates over all three correct durations therefore produce the requested extremes.

Complexity: O(1) time and O(1) extra space: the input has a fixed number of values.

Watch out. A negative difference indicates midnight crossing. Print 2:00 rather than 2:0. The clock data alone cannot identify extra whole days.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
int main(){
    int shortest=1440;
    int longest=0;
    for(int car=0;car<3;car++){
        int startHour,startMinute,endHour,endMinute;
        cin>>startHour>>startMinute>>endHour>>endMinute;
        int duration=60*endHour+endMinute-60*startHour-startMinute;
        if(duration<0){
            duration+=24*60;
        }
        shortest=min(shortest,duration);
        longest=max(longest,duration);
    }
    // Only the minutes need a leading zero.
    cout<<shortest/60<<':'<<setw(2)<<setfill('0')<<shortest%60<<'\n';
    cout<<longest/60<<':'<<setw(2)<<setfill('0')<<longest%60<<'\n';
}

Source: Arena Olimpiici ↗