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 E2022 · MediumTime arithmetic

Hour Hand Reaches a Mark

Autumn Tournament (2022)

Submission name: clock.cpp

Problem summary

A clock shows h hours and m minutes. Mark the current position of the minute hand. Find the first strictly later moment when the hour hand reaches this fixed mark. Report the clock reading and the elapsed hours and minutes.

Input

Two integers h and m.

Output

First print the target hour (1–12) and minute. Then print elapsed hours and minutes.

Constraints

1 ≤ h ≤ 12; 0 ≤ m < 60.

Worked example

Original statement, example 1.

Input

3 25

Output

5 0
1 35

Solution analysis

  1. Measure time in minutes from 12:00 in a 720-minute cycle. The initial reading is start = (h % 12) × 60 + m.
  2. The marked minute-scale position m is reached by the hour hand at target = 12m minutes from 12:00, because the hour hand needs 12 minutes to advance one minute mark. If target is not strictly after start, add 720 to its occurrence time.
  3. For 3:25, start = 205 and target = 300, corresponding to 5:00. The wait is 95 minutes, printed as 1 35. At 12:00 the next occurrence is a full 12 hours later.

Why it works

The hour hand moves uniformly through 60 minute marks every 720 minutes, so it reaches mark m exactly at times congruent to 12m modulo 720. Choosing the first such time strictly greater than start gives exactly the requested moment; quotient and remainder convert both values to hours and minutes.

Complexity: O(1) time and O(1) extra space.

Watch out. The marked position is fixed. If both hands initially meet at that position, the requested moment is still strictly later. Display hour zero as 12.

Commented C++17 solution

An independently written complete program with standard headers.

// Key idea: The hour hand moves uniformly through 60 minute marks every 720 minutes, so it reaches
// mark m exactly at times congruent to 12m modulo 720. Choosing the first such time strictly greater
// than start gives exactly the requested moment; quotient and remainder convert both values to hours
// and minutes.
#include<iostream>
using namespace std;
int main(){
    int h,m;
    cin>>h>>m;
    int start=(h%12)*60+m;
    int target=12*m;
    int elapsed=target-start;
    if(elapsed<=0){
        elapsed+=720;
    }
    int hour=target/60;
    if(hour==0){
        hour=12;
    }
    cout<<hour<<' '<<target%60<<'\n';
    cout<<elapsed/60<<' '<<elapsed%60<<'\n';
}

Source: Arena Olimpiici ↗