Problem Breakdown
We are given a specific moment in time on an analog clock defined by hour and minutes. The goal is to calculate the smaller angle (in degrees) between the hour hand and the minute hand at that exact instant.
Inputs:
hour: Integer, range[1, 12]. Represents the current hour mark.minutes: Integer, range[0, 59]. Represents the minutes past the hour.
Outputs:
- A float representing the angle in degrees between . We want the smaller of the two possible angles formed by the hands (the reflex angle is ignored).
Implicit Constraints & Edge Cases:
- Hour Hand Movement: The hour hand does not stay fixed at an integer mark. It moves continuously as minutes pass. For example, at
hour=1,minutes=30, the hour hand has moved halfway to 2 o’clock. - Clock Cycle: A clock is circular (). An angle of wraps around; we need the geometric shortest path between hands.
- Hour = 12: Mathematically, 12 behaves like 0 in a modulo arithmetic context for position calculations relative to the top vertical axis (12 o’clock), but input constraints say
houris . We must handle this mapping correctly so that 12 and 0 are treated as equivalent positions.
Intuition
A brute force approach might involve simulating every second or minute incrementally to track hand movement, which would be computationally inefficient ( where N is minutes) and prone to floating-point drift errors if not careful with precision. Since the problem asks for a specific mathematical state based on inputs, we can solve this in constant time using geometry.
Key Insight:
- Minute Hand Position: The minute hand completes in 60 minutes. Therefore, it moves at a rate of per minute. Its position depends solely on
minutes. - Hour Hand Position: The hour hand completes in 12 hours (720 minutes). Therefore, it moves at a rate of per minute. Its position depends on the current
hourand advances based onminutes.- Base angle for hour hand:
(hour % 12) * 30(since it moves per full hour). - Incremental movement due to minutes:
minutes * 0.5.
- Base angle for hour hand:
Optimal Approach: Calculate the absolute position of both hands relative to the 12 o’clock mark, find the difference between them, and ensure we return the smaller angle (min(diff, -diff)).
Approach
-
Calculate Minute Hand Angle (
angle_m): Multiplyminutesby 6 degrees per minute. This gives the position relative to 12 o’clock (). -
Calculate Hour Hand Angle (
angle_h):- First, normalize
hourusing modulo 12 so that 12 becomes 0 (since 12 is geometrically equivalent to 0 on the circle for calculation purposes relative to minute hand movement starting from top). - Multiply normalized hour by 30 degrees per full hour.
- Add the incremental shift caused by minutes:
minutes * 0.5.
- First, normalize
-
Calculate Difference: Compute absolute difference between
angle_handangle_m. Let this bediff. -
Normalize Angle: The angle on a clock is circular. If
diffexceeds , the smaller angle lies in the other direction (). Return .
Complexity Analysis
- Time Complexity: . We perform a fixed number of arithmetic operations regardless of input size.
- Space Complexity: . We only use variables to store intermediate angle values; no additional data structures are allocated.
Solution
def angleBetweenHands(hour: int, minutes: int) -> float:
# Calculate the position of the minute hand in degrees from 12 o'clock
# The minute hand moves 360 degrees per hour (60 mins), so 6 deg/min
minute_angle = minutes * 6.0
# Calculate the position of the hour hand in degrees from 12 o'clock
# Hour hand moves 360 degrees per 12 hours -> 30 deg/hour or 0.5 deg/minute
# We use (hour % 12) to handle '12' as equivalent to '0' for calculation base
hour_angle = ((hour % 12) * 30.0) + (minutes * 0.5)
# Find the absolute difference between the two angles
diff = abs(hour_angle - minute_angle)
# The smaller angle is either the direct difference or the reflex complement
return min(diff, 360.0 - diff)
# Example usage for verification:
print(angleBetweenHands(12, 30)) # Output: 165.0
print(angleBetweenHands(3, 30)) # Output: 75.0
Edge Cases Handled
- Hour = 12: The modulo operation
(hour % 12)correctly maps12to0, ensuring the hour hand starts at the top of the circle rather than calculating a full offset unnecessarily, though mathematically equivalent. - Angle > 180 degrees: By returning
min(diff, 360 - diff), we guarantee the result is always , satisfying the requirement for the “smaller angle”. - Minute = 0 or 59: The linear calculation handles these naturally without special conditional logic.
- Floating Point Precision: Python’s standard float precision (double precision) provides sufficient accuracy to meet the tolerance constraint specified in the problem description.