Angle Between Hands of a Clock (LeetCode 1344): Solution and Explanation
Given a time as hour and minutes, return the smaller angle in degrees between the hour hand and the minute hand of an analog clock. Answers within 10^-5 of the true value are accepted.
Intuition
Place both hands on the same scale: degrees measured clockwise from the 12. Once each hand is a single number, the gap between them is just a subtraction.
The minute hand sweeps the full 360 degrees every 60 minutes, so it moves 6 degrees per minute and sits at minutes * 6.
The hour hand is the easy one to get wrong. It does not jump from mark to mark; it moves smoothly, covering 30 degrees per hour, which works out to half a degree per minute. At 3:30 it is halfway between 3 and 4, at 3 * 30 + 30 * 0.5 = 105 degrees. Also, 12 o'clock is the top of the dial, so use hour % 12 to turn 12 into 0.
The absolute difference of the two angles gives the arc going one way around the dial. The arc going the other way is 360 minus that. The question asks for the smaller one.
Approach: Angle of each hand
- Compute
minute_angle = minutes * 6. - Compute
hour_angle = (hour % 12) * 30 + minutes * 0.5. - Let
diff = abs(hour_angle - minute_angle), the arc in one direction. - Return
min(diff, 360 - diff), the smaller of the two arcs.
Step-by-step example
hour = 12, minutes = 30
- minute_angle = 30 * 6 = 180 degrees, pointing straight down at the 6.
- hour_angle = (12 % 12) * 30 + 30 * 0.5 = 0 + 15 = 15 degrees, halfway between 12 and 1.
- diff = |15 - 180| = 165 degrees.
- The other arc is 360 - 165 = 195 degrees.
- min(165, 195) = 165, so the answer is 165.0.
Code: Python, Java, C++
1class Solution:
2 def angleClock(self, hour: int, minutes: int) -> float:
3 minute_angle = minutes * 6
4 hour_angle = (hour % 12) * 30 + minutes * 0.5
5 diff = abs(hour_angle - minute_angle)
6 return min(diff, 360 - diff)1class Solution {
2 public double angleClock(int hour, int minutes) {
3 double minuteAngle = minutes * 6;
4 double hourAngle = (hour % 12) * 30 + minutes * 0.5;
5 double diff = Math.abs(hourAngle - minuteAngle);
6 return Math.min(diff, 360 - diff);
7 }
8}1class Solution {
2public:
3 double angleClock(int hour, int minutes) {
4 double minuteAngle = minutes * 6;
5 double hourAngle = (hour % 12) * 30 + minutes * 0.5;
6 double diff = fabs(hourAngle - minuteAngle);
7 return min(diff, 360 - diff);
8 }
9};Time and space complexity
O(1)
A fixed handful of multiplications, one subtraction and one comparison.
O(1)
Only three numbers are stored.
Other approaches
Relative speed (time O(1), space O(1))
The minute hand gains 5.5 degrees per minute on the hour hand. The gap is |30 * (hour % 12) - 5.5 * minutes|, then take the smaller arc. It is the same formula after combining terms.
Work in half degrees (time O(1), space O(1))
Multiply every angle by 2 so all values are integers (hour % 12 * 60 + minutes and minutes * 12), do the comparison in integers, and divide by 2 at the end. It avoids floating point until the last step.
Common mistakes
- Leaving the hour hand on its mark. At 3:15 the hour hand is at 97.5 degrees, not 90, so the answer is 7.5, not 0.
- Assuming the answer must be a whole number and returning an int. The half degree per minute of the hour hand makes answers like 7.5 common.
- Returning the absolute difference without comparing to
360 - diff. At 1:57 the direct difference is 283.5, but the smaller arc is 76.5. - Using integer division for
minutes / 2in Java, C++ or C#, which drops the half degree. Multiply by 0.5 instead.
Frequently asked questions
How fast does each hand move?
The minute hand moves 6 degrees per minute. The hour hand moves 30 degrees per hour, which is 0.5 degrees per minute.
Why take min(diff, 360 - diff)?
Two hands split the dial into two arcs that add up to 360 degrees. The absolute difference is one of them; the problem wants the smaller.
Can the answer be a fraction?
Yes. The hour hand moves half a degree per minute, so answers are always multiples of 0.5, such as 7.5 at 3:15.
What is the time complexity?
O(1) time and O(1) space, since the answer is a closed-form formula.