Angle Between Hands of a Clock: Simple Math
1344. Angle Between Hands of a Clock gives you an hour (1 through 12) and minutes (0 through 59) and asks you to return the smaller angle in degrees formed between the hour and the minute hand.
Once you know how fast each hand moves, the problem reduces to three lines of arithmetic.
The math behind the hands
Both hands sweep around a 360-degree circle, but at different speeds.
Minute hand: it completes a full revolution every 60 minutes. That means it moves 360 / 60 = 6 degrees per minute. At m minutes past the hour, the minute hand sits at m * 6 degrees from the 12 o'clock position.
Hour hand: it completes a full revolution every 12 hours. Each hour mark is 360 / 12 = 30 degrees apart. But the hour hand does not jump from one mark to the next. It moves continuously. Every minute it advances by 30 / 60 = 0.5 degrees. So at h hours and m minutes, the hour hand sits at (h % 12) * 30 + m * 0.5 degrees from the 12 o'clock position.
The % 12 handles the case where h = 12, which should be treated as 0 (same position as the top of the clock).
Python solution
def angleClock(hour: int, minutes: int) -> float:
minute_angle = minutes * 6
hour_angle = (hour % 12) * 30 + minutes * 0.5
diff = abs(minute_angle - hour_angle)
return min(diff, 360 - diff)
That is the entire solution. No loops, no data structures, just formulas.
Step-by-step walkthrough: 12:30
Let's trace through each calculation to see exactly how the formulas work.
angleClock(12, 30):Step 1: Calculate the minute hand angle
minutes * 630 * 6 = 180180.0 degreesEach minute moves the minute hand 6 degrees (360 / 60 = 6). At 30 minutes the hand points straight down.
Step 2: Calculate the hour hand angle
(hour % 12) * 30 + minutes * 0.5(12 % 12) * 30 + 30 * 0.50 + 15 = 1515.0 degreesEach hour moves the hand 30 degrees, and each minute adds 0.5 degrees. Hour 12 wraps to 0 via modulo, then the 30 minutes push it to 15 degrees.
Step 3: Compute the raw difference
abs(minute_angle - hour_angle)abs(180.0 - 15.0) = 165.0165.0 degreesTake the absolute difference between the two angles.
Step 4: Return the smaller angle
min(diff, 360 - diff)min(165.0, 195.0) = 165.0Result: the angle between the hands at 12:30 is 165.0 degrees.
At 12:30, the minute hand points at 180 degrees (straight down) and the hour hand has moved 15 degrees past the 12. The raw difference is 165 degrees. Since 165 is already less than 195 (which is 360 - 165), we return 165.0.
Why take min(diff, 360 - diff)?
Two hands on a clock always create two arcs: one going clockwise and one going counterclockwise. Together they add up to 360 degrees. The problem asks for the smaller of the two.
If the raw difference diff between the two angles is more than 180 degrees, then the shorter path around the clock is 360 - diff. Taking min(diff, 360 - diff) always gives you the smaller angle regardless of which hand is "ahead."
Complexity analysis
| Metric | Value |
|---|---|
| Time | O(1). A fixed number of arithmetic operations, independent of the input values. |
| Space | O(1). Only a few numeric variables. |
Building blocks
This problem is built on two reusable ideas that show up in other math problems.
1. Angle computation from rates
When an object moves at a constant rate around a circle, its position at time t is rate * t degrees. The minute hand moves at 6 degrees per minute, the hour hand at 0.5 degrees per minute. This same idea applies to any circular motion problem.
2. Modular arithmetic on a circle
Angles on a clock wrap around at 360 degrees. The % 12 on the hour and the min(diff, 360 - diff) at the end are both examples of working with values that wrap. You will see this wrapping logic in problems involving circular arrays, rotations, and other modular arithmetic tasks.
Edge cases
- 12:00: Both hands point at 0 degrees. The angle is 0.
- 6:00: The minute hand is at 0 degrees and the hour hand is at 180 degrees. The angle is 180.
- 12:30: The minute hand is at 180 degrees and the hour hand is at 15 degrees. The angle is 165. This case catches mistakes where you forget the hour hand moves between hour marks.
A common mistake is treating the hour hand as if it only moves in 30-degree jumps. At 12:30, the hour hand is not still at the 12. It has moved halfway toward the 1, sitting at 15 degrees. The minutes * 0.5 term accounts for this continuous movement.
From understanding to recall
This problem looks simple once you see the formulas. But in an interview, remembering the exact rates (6 degrees per minute for the minute hand, 0.5 for the hour hand) and the wrapping logic (min(diff, 360 - diff)) matters. If you mix up 6 and 0.5, or forget the % 12, you get the wrong answer.
Spaced repetition helps you internalize these small details. CodeBricks drills the building blocks (angle-from-rate, modular wrapping) separately so that when they appear together in a problem like this, you produce the correct formulas without hesitation.
Related posts
- Palindrome Number - Another math problem where modular arithmetic plays a key role in extracting and comparing digits
- Reverse Integer - Digit-by-digit arithmetic with careful handling of edge cases and overflow
- Base 7 - Converting between number representations using repeated division, another core math building block