cppAcademy
FROM LEARNING TO COMPETING

Intermediate.
Put your skills to the test.

Bulgarian Informatics Group D · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group D also includes challenging national-level problems. The six 2017 National tasks shared by Groups C and D use their existing permanent URLs. Years follow Arena’s competition-season labels. Source clarifications and special grader requirements are noted on affected tasks.
GROUP D2009 · MediumModular arithmetic

Whole Minutes until Clock Hands Meet

National Olympiad — National round (2009 season)

Submission name: clock.cpp

Problem summary

Find the number of full minutes until the nearest overlap of analogue clock hands at exact time h:m. A current overlap is allowed.

Input

h m.

Output

The floor of the waiting time in minutes.

Constraints

0 ≤ h ≤11;0 ≤ m ≤59.

Worked example

Input and output for the worked example discussed below.

Input

3 18

Output

63

Solution analysis

  1. In half-degree units the hour-minus-minute angular gap is60h−11m. The minute hand gains11 half-degrees per minute, so no floating-point angles are needed.
  2. Reduce this gap modulo720, one full revolution. Divide the nonnegative gap by11 using integer division to count complete minutes.
  3. At3:18 the gap−18 becomes702 and floor(702/11)=63. At3:16 the wait4/11 minutes gives0. At0:00 the immediate overlap also gives0.

Why it works

Uniform relative motion covers the reduced gap at11 half-degrees per minute. The reduced nonnegative gap corresponds to the nearest meeting, and integer division gives its full-minute part.

Complexity: O(1) time and space.

Watch out. Do not round up or ignore hour-hand motion. Do not skip an overlap at the current instant.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
using namespace std;
int main(){
    int hour;
    int minute;
    cin>>hour>>minute;
    // Exact half-degree arithmetic includes motion of both hands.
    int gap=(60*hour-11*minute+720)%720;
    cout<<gap/11<<'\n';
}

Source: Arena Olimpiici ↗