Angle Between Hands of a Clock Problem
#1344
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 1344.
Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand.
Answers within 10^(-5) of the actual value will be accepted as correct.
Example 1:

Input: hour = 12, minutes = 30
Output: 165
Example 2:

Input: hour = 3, minutes = 30
Output: 75
Example 3:

Input: hour = 3, minutes = 15
Output: 7.5
Constraints:
1 <= hour <= 120 <= minutes <= 59
Solution
Approach: Math
Measure both hands in degrees clockwise from 12:
- The minute hand goes around once per hour:
6degrees per minute, so it is at6 * minutes. - The hour hand moves
30degrees per hour, and it also moves while the minutes pass:30 / 60 = 0.5degrees per minute. It is at30 * (hour % 12) + 0.5 * minutes. Takinghour % 12puts 12 o'clock at0.
The difference diff = |hour angle - minute angle| is one of the two angles between the hands, and 360 - diff is the other. Return the smaller one. Forgetting that the hour hand moves with the minutes is the usual mistake: at 3:15 the hands are not on top of each other, they are 7.5 degrees apart.
Complexity: O(1) time and space.
class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
minute_angle = 6 * minutes # 360 / 60 degrees per minute
hour_angle = 30 * (hour % 12) + 0.5 * minutes # 30 per hour, plus 0.5 per minute
diff = abs(hour_angle - minute_angle)
return min(diff, 360 - diff)