Angle Between Hands of a Clock

Medium Arrays & Hashing Math / Number Theory

Angle Between Hands of a Clock is a medium arrays & hashing problem solved with the math / number theory pattern. The best approach, optimal (one formula in half degrees), runs in O(1) time and O(1) space. Below are 3 approaches in Java, Python, C++ and JavaScript, from brute force (tick the clock minute by minute) up.

Problem

A clock with an hour hand and a minute hand shows the time hour:minutes. Return the smaller angle, in degrees, between the two hands.

Both hands move smoothly: as the minutes pass, the hour hand drifts from one hour mark toward the next, so at 3:30 it sits halfway between the 3 and the 4. The two hands form two angles that add up to 360 degrees; return the one that is at most 180. Answers within 10^-5 of the true angle are accepted.

Examples

Example 1

Input
hour = 3, minutes = 40
Output
130.0
Why
The minute hand is at 40 × 6 = 240 degrees. The hour hand is at 3 × 30 + 40 × 0.5 = 110 degrees. They are 130 degrees apart, and the other way round is 230, so the answer is 130.

Example 2

Input
hour = 12, minutes = 6
Output
33.0
Why
The minute hand is at 36 degrees. Twelve o'clock counts as 0, so the hour hand has moved only 6 × 0.5 = 3 degrees. The gap is 33 degrees.

Example 3

Input
hour = 9, minutes = 0
Output
90.0
Why
The hands are at 270 and 0 degrees. That is 270 degrees one way and 90 the other, so the smaller angle is 90.

Constraints

  • 1 <= hour <= 12
  • 0 <= minutes <= 59
  • Answers within 10^-5 of the true angle are accepted.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (3 examples, or your own). Submit also runs 12 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Brute force (tick the clock minute by minute)O(hour · 60 + minutes)O(1)
Direct anglesO(1)O(1)
Optimal (one formula in half degrees)O(1)O(1)

1Brute force (tick the clock minute by minute)

TimeO(hour · 60 + minutes)One tick per minute since 12:00, which is at most 719 ticks.
SpaceO(1)Two angles and a counter.

Start both hands at 12, where each angle is 0, and run the clock forward one minute at a time until it shows the given time. Each tick adds 6 degrees to the minute hand and half a degree to the hour hand, wrapping past 360. At the end, compare the two angles.

  1. The time since 12:00 is total = (hour % 12) · 60 + minutes minutes.
  2. Set h = 0 and m = 0. Repeat total times: m = (m + 6) mod 360 and h = h + 0.5.
  3. Let d = |h - m|. Return min(d, 360 - d).
class Solution {
    public double angleClock(int hour, int minutes) {
        int total = (hour % 12) * 60 + minutes;
        double h = 0, m = 0;
        for (int t = 0; t < total; t++) {
            m = (m + 6) % 360;
            h += 0.5;
        }
        double d = Math.abs(h - m);
        return Math.min(d, 360 - d);
    }
}

2Direct angles

TimeO(1)A handful of arithmetic operations.
SpaceO(1)Three numbers.

Skip the ticking and place each hand directly. The minute hand is at 6 · minutes degrees. The hour hand is at 30 · (hour mod 12) for the full hours, plus 0.5 · minutes for the part of the hour that has passed. The answer is the smaller of the gap and 360 minus the gap.

  1. minuteAngle = 6 · minutes.
  2. hourAngle = 30 · (hour % 12) + 0.5 · minutes.
  3. d = |hourAngle - minuteAngle|; return min(d, 360 - d).
class Solution {
    public double angleClock(int hour, int minutes) {
        double minuteAngle = 6.0 * minutes;
        double hourAngle = 30.0 * (hour % 12) + 0.5 * minutes;
        double d = Math.abs(hourAngle - minuteAngle);
        return Math.min(d, 360 - d);
    }
}

3Optimal (one formula in half degrees)

TimeO(1)Two multiplications, a subtraction and a comparison.
SpaceO(1)One integer.

Subtract the two angles by hand: 30h + 0.5m - 6m = 30h - 5.5m. Doubling everything removes the fraction, so the gap measured in half degrees is |60h - 11m| with h = hour mod 12. A full turn is 720 half degrees. Everything stays an exact integer until the final division by 2.

  1. h = hour % 12.
  2. d = |60 · h - 11 · minutes|, the gap in half degrees.
  3. d = min(d, 720 - d).
  4. Return d / 2.0.
class Solution {
    public double angleClock(int hour, int minutes) {
        int d = Math.abs(60 * (hour % 12) - 11 * minutes);
        d = Math.min(d, 720 - d);
        return d / 2.0;
    }
}

Edge cases to test

  • 12:00, both hands on top of each other (0)
  • 6:00, hands exactly opposite (180)
  • hour = 12 must behave like 0
  • Times just before the hour, like 11:59, where the hour hand is almost at the next number
  • A gap bigger than 180 must be turned into 360 minus the gap

Hints

Hint 1

Measure both hands in degrees, clockwise from the 12 mark.

Hint 2

The minute hand moves 360 / 60 = 6 degrees per minute. The hour hand moves 360 / 12 = 30 degrees per hour, and it also creeps 30 / 60 = 0.5 degrees per minute.

Hint 3

Take the absolute difference d of the two angles. The smaller angle is min(d, 360 - d).

FAQ

What is the best time complexity for Angle Between Hands of a Clock?

Optimal (one formula in half degrees) runs in O(1) time and O(1) extra space. Two multiplications, a subtraction and a comparison.

Which pattern does Angle Between Hands of a Clock use?

It is a arrays & hashing problem that uses the math / number theory pattern. Other problems with the same pattern: Bulb Switcher, Reaching Points, Water and Jug Problem.

Is there a brute force solution for Angle Between Hands of a Clock?

Yes. Brute force (tick the clock minute by minute) takes O(hour · 60 + minutes) time and O(1) space. Start both hands at 12, where each angle is 0, and run the clock forward one minute at a time until it shows the given time.

Which edge cases should I test for Angle Between Hands of a Clock?

12:00, both hands on top of each other (0); 6:00, hands exactly opposite (180); hour = 12 must behave like 0; Times just before the hour, like 11:59, where the hour hand is almost at the next number; A gap bigger than 180 must be turned into 360 minus the gap.