Skip to content
DSAPrep

    ↑↓ to move · Enter to open · Esc to close

    Angle Between Hands of a Clock

    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.

    A clock at 12:30 with the minute hand on 6 and the hour hand halfway between 12 and 1.
    A clock at 3:30 with the minute hand on 6 and the hour hand between 3 and 4.
    A clock at 3:15 with the minute hand on 3 and the hour hand a quarter of the way past 3.

    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 <= 12
    • 0 <= minutes <= 59
    View original on LeetCode ↗

    Two hands race around the same dial at two different constant speeds. The trick is to stop thinking about a clock and start thinking about a 360-degree circle: convert each hand to its angle, subtract, and then remember that two radii always cut the circle into two arcs whose sizes add up to 360. The answer is the smaller arc.

    First Idea: Simulate the Hands Minute by Minute

    Time O(t)Space O(1)

    A clock is just two objects moving at constant speed, so the most literal approach is to run the motion. Start at 12:00 with both hands on top, then advance one minute at a time: the minute hand gains 6 degrees, the hour hand gains 0.5 degrees, and both wrap around past 360. After t = (hour % 12) * 60 + minutes ticks the hands sit exactly where the input says they should, and the rest is subtraction.

    class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
    # Both hands start together at 12:00.
    hour_angle = 0.0
    minute_angle = 0.0
    for _ in range((hour % 12) * 60 + minutes):
    hour_angle = (hour_angle + 0.5) % 360.0
    minute_angle = (minute_angle + 6.0) % 360.0
    diff = abs(hour_angle - minute_angle)
    return min(diff, 360.0 - diff)

    Why it works, and what it costs: the loop reproduces the definition of a clock, so it cannot be wrong about the rates — in particular, the 0.5-degree-per-minute creep of the hour hand is impossible to forget. It runs t steps where t = (hour % 12) * 60 + minutes, so the time is O(t) (at most 719 iterations) and the space is O(1) because only two angles are tracked. Keeping both angles in [0, 360) with % 360.0 is what makes the final subtraction and its complement meaningful.

    Closed Form: Rate Times Time

    OptimalTime O(1)Space O(1)

    The loop exists only to accumulate a linear function, so evaluate it directly. The hour hand starts at 30 * hour degrees and creeps 0.5 degrees for every elapsed minute, so hourAngle = 30 * hour + 0.5 * minutes. The minute hand is simpler: minuteAngle = 6 * minutes. Their absolute difference is one arc between the hands; because the two arcs fill the dial, the other one is 360 - diff. The answer is min(diff, 360 - diff).

    class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
    hour_angle = 30 * hour + 0.5 * minutes
    minute_angle = 6 * minutes
    diff = abs(hour_angle - minute_angle)
    return min(diff, 360 - diff)

    Why it works: each hand’s angle is an affine function of time, so no iteration is needed. Subtracting gives one of the two arcs, and since the two arcs sum to 360, taking the minimum of the difference and its complement always returns the smaller angle — even when the raw gap exceeds 180, as at 12:30 where 195 collapses to 165. Twelve o’clock contributes 360 degrees rather than 0, which is the same direction and is handled by the same min. Time and space are both O(1): a fixed number of arithmetic operations on two numbers.

    Watch the hands land on their degree marks while the arithmetic builds up, then the gap reduce to the smaller arc.

    start12:30
    12369

    indigo = hour hand, amber = minute hand, green = measured arc

    hour hand—
    minute hand—
    raw gap—
    other arc—
    smaller—

    the two arcs always add up to 360°, so the answer is the smaller one

    1 / 6
    currentcomparingresult

    Both hands sweep the dial at constant rates. Place each one at its exact degree mark measured clockwise from 12, then measure the gap between them.

    A second reading, hour = 3, minutes = 15, where the raw gap is already the small arc:

    start3:15
    12369

    indigo = hour hand, amber = minute hand, green = measured arc

    hour hand—
    minute hand—
    raw gap—
    other arc—
    smaller—

    the two arcs always add up to 360°, so the answer is the smaller one

    1 / 6
    currentcomparingresult

    Both hands sweep the dial at constant rates. Place each one at its exact degree mark measured clockwise from 12, then measure the gap between them.

    More Math & Geometry practice