Domain 2MediumMEDIUM

Angle Between Hands of a Clock

LeetCode problem 1344: Angle Between Hands of a Clock (medium).

Domain: medium•Updated: 2026-09-14
#Math

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 [0,180][0, 180]. 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 (360∘360^\circ). An angle of 450∘450^\circ 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 hour is [1,12][1, 12]. 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 (O(N)O(N) 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:

  1. Minute Hand Position: The minute hand completes 360∘360^\circ in 60 minutes. Therefore, it moves at a rate of 36060=6∘\frac{360}{60} = 6^\circ per minute. Its position depends solely on minutes.
  2. Hour Hand Position: The hour hand completes 360∘360^\circ in 12 hours (720 minutes). Therefore, it moves at a rate of 360720=0.5∘\frac{360}{720} = 0.5^\circ per minute. Its position depends on the current hour and advances based on minutes.
    • Base angle for hour hand: (hour % 12) * 30 (since it moves 30∘30^\circ per full hour).
    • Incremental movement due to minutes: minutes * 0.5.

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, 360360-diff)).

Approach

  1. Calculate Minute Hand Angle (angle_m): Multiply minutes by 6 degrees per minute. This gives the position relative to 12 o’clock (0∘0^\circ).

  2. Calculate Hour Hand Angle (angle_h):

    • First, normalize hour using 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.
  3. Calculate Difference: Compute absolute difference between angle_h and angle_m. Let this be diff.

  4. Normalize Angle: The angle on a clock is circular. If diff exceeds 180∘180^\circ, the smaller angle lies in the other direction (360−diff360 - \text{diff}). Return min⁡(diff,360−diff)\min(\text{diff}, 360 - \text{diff}).

Complexity Analysis

  • Time Complexity: O(1)O(1). We perform a fixed number of arithmetic operations regardless of input size.
  • Space Complexity: O(1)O(1). 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 maps 12 to 0, ensuring the hour hand starts at the top of the circle rather than calculating a full 360∘360^\circ offset unnecessarily, though mathematically equivalent.
  • Angle > 180 degrees: By returning min(diff, 360 - diff), we guarantee the result is always ≤180\leq 180, 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 10−510^{-5} tolerance constraint specified in the problem description.