DDSA Solutions

1344. Angle Between Hands of a Clock

Time: O(1)
Space: O(1)

Problem Overview

A clock face is 360° with 12 hour marks (30° apart) and 60 minute marks (6° apart).

Advertisement

Intuition

A clock face is 360° with 12 hour marks (30° apart) and 60 minute marks (6° apart). The hour hand moves continuously: at hour h and minute m it sits at (h mod 12 + m/60) × 30 degrees. The minute hand sits at m × 6 degrees. The angle between them is the absolute difference; return the smaller of that difference and 360 minus the difference to get the acute angle.

Algorithm

  1. 1Compute hourHand = (hour % 12 + minutes / 60.0) * 30.
  2. 2Compute minuteHand = minutes * 6.0.
  3. 3Set diff = |hourHand - minuteHand|.
  4. 4Return min(diff, 360 - diff).

Example Walkthrough

Input: hour = 12, minutes = 30

  1. 1.hourHand = (0 + 30/60) × 30 = 15°.
  2. 2.minuteHand = 30 × 6 = 180°.
  3. 3.diff = |15 - 180| = 165°.
  4. 4.min(165, 195) = 165°.

Output: 165

Common Pitfalls

  • Use hour % 12 so 12 o'clock is treated as 0 for position math.
  • Include minutes in the hour-hand position (fractional hour).
  • Return the smaller angle, not always the raw difference.
1344.cs
C#
// Approach: Each clock hand moves at a constant degrees-per-minute rate.
// Hour hand: (hour % 12 + minutes / 60.0) * 30 — 30° per hour plus fractional hour from minutes.
// Minute hand: minutes * 6 — 6° per minute.
// Take the absolute difference and return the smaller of diff and 360 - diff (acute angle).
// Time: O(1) Space: O(1)
public class Solution
{
    public double AngleClock(int hour, int minutes)
    {
        double hourHand = (hour % 12 + minutes / 60.0) * 30;
        double minuteHand = minutes * 6;
        double diff = Math.Abs(hourHand - minuteHand);
        return Math.Min(diff, 360 - diff);
    }
}
Advertisement
Was this solution helpful?