Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 1344. Angle Between Hands of a Clock

[LeetCode Link](https://leetcode.com/problems/angle-between-hands-of-a-clock/)

Difficulty: Medium
Topics: Math

Acceptance Rate: 66.1%

## Hints

### Hint 1

This is a pure math problem — there is no data structure or search involved. The whole challenge is translating "where the hands point" into angles. Think about each hand independently: if you can compute the absolute angle (measured clockwise from the 12 o'clock position) of the minute hand and of the hour hand, the answer is just the gap between them.

### Hint 2

Convert a full revolution (360 degrees) into the units each hand uses. The minute hand sweeps the whole clock in 60 minutes, so each minute is worth `360 / 60 = 6` degrees. The hour hand sweeps the whole clock in 12 hours, so each hour is worth `360 / 12 = 30` degrees. The tricky part is the hour hand: it does not jump from hour to hour — it drifts continuously as the minutes pass.

### Hint 3

The hour hand moves a little every minute. In 60 minutes it covers 30 degrees, so it gains `0.5` degrees per minute. That means:

- minute angle = `minutes * 6`
- hour angle = `(hour % 12) * 30 + minutes * 0.5`

Take the absolute difference of the two angles. Because the difference might be the "long way around" (greater than 180), the smaller angle is `min(diff, 360 - diff)`. Don't forget `hour % 12` so that 12 maps to 0.

## Approach

The clock face is a circle of 360 degrees. We measure each hand's position as an angle clockwise from the 12 o'clock mark, then take the smaller of the two arcs between them.

Step by step:

1. **Minute hand.** It completes one full circle (360 degrees) every 60 minutes, so it advances `6` degrees per minute. Its angle is `minuteAngle = minutes * 6`.

2. **Hour hand.** It completes one full circle every 12 hours, so it advances `30` degrees per hour. Critically, it also keeps moving during the hour: across 60 minutes it covers those 30 degrees, i.e. `0.5` degrees per minute. We also reduce the hour modulo 12 so that `12` o'clock is treated as `0`. Its angle is `hourAngle = (hour % 12) * 30 + minutes * 0.5`.

3. **Difference.** Compute `diff = |hourAngle - minuteAngle|`. This is one of the two arcs separating the hands; the other arc is `360 - diff`. The answer is the smaller one: `min(diff, 360 - diff)`.

**Worked example** (`hour = 12, minutes = 30`):

- `minuteAngle = 30 * 6 = 180`
- `hourAngle = (12 % 12) * 30 + 30 * 0.5 = 0 + 15 = 15`
- `diff = |15 - 180| = 165`, and `360 - 165 = 195`, so the answer is `min(165, 195) = 165`. ✅

This matches the expected output, and the same formula cleanly produces `75` for `3:30` and `7.5` for `3:15`.

## Complexity Analysis

Time Complexity: O(1) — a fixed number of arithmetic operations regardless of input.
Space Complexity: O(1) — only a couple of scalar values are stored.

## Edge Cases

- **`hour = 12`:** Without `hour % 12`, the hour hand would be computed as `12 * 30 = 360` degrees instead of `0`. The modulo keeps 12 o'clock at the top of the clock.
- **Continuous hour-hand drift:** Forgetting the `minutes * 0.5` term is the most common bug. At `3:30` the hour hand is halfway between 3 and 4, not exactly on 3 — leaving out the drift would give the wrong answer.
- **Reflex angle (long way around):** When the raw difference exceeds 180 degrees, the true "smaller angle" is `360 - diff`. Always taking the minimum of the two arcs handles this.
- **`minutes = 0`:** Both hands sit on exact hour marks; the formula still works (e.g. `3:00` gives `90`).
- **Floating-point tolerance:** Answers within `10^-5` are accepted, so results like `7.5` should be compared with a small epsilon rather than for exact equality.
57 changes: 57 additions & 0 deletions problems/1344-angle-between-hands-of-a-clock/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
number: "1344"
frontend_id: "1344"
title: "Angle Between Hands of a Clock"
slug: "angle-between-hands-of-a-clock"
difficulty: "Medium"
topics:
- "Math"
acceptance_rate: 6610.3
is_premium: false
created_at: "2026-06-18T05:14:21.569295+00:00"
fetched_at: "2026-06-18T05:14:21.569295+00:00"
link: "https://leetcode.com/problems/angle-between-hands-of-a-clock/"
date: "2026-06-18"
---

# 1344. 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.



**Example 1:**

![](https://assets.leetcode.com/uploads/2019/12/26/sample_1_1673.png)


**Input:** hour = 12, minutes = 30
**Output:** 165


**Example 2:**

![](https://assets.leetcode.com/uploads/2019/12/26/sample_2_1673.png)


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


**Example 3:**

![](https://assets.leetcode.com/uploads/2019/12/26/sample_3_1673.png)


**Input:** hour = 3, minutes = 15
**Output:** 7.5




**Constraints:**

* `1 <= hour <= 12`
* `0 <= minutes <= 59`
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "math"

// angleClock computes the smaller angle (in degrees) between the hour and
// minute hands of a clock.
//
// Approach (pure math, O(1)):
// - The minute hand moves 360/60 = 6 degrees per minute.
// - The hour hand moves 360/12 = 30 degrees per hour, plus 0.5 degrees per
// minute to account for its continuous drift within the hour.
// - We reduce the hour modulo 12 so that 12 o'clock maps to 0 degrees.
// - The two hands divide the circle into two arcs; we return the smaller,
// which is min(diff, 360 - diff).
func angleClock(hour int, minutes int) float64 {
minuteAngle := float64(minutes) * 6.0
hourAngle := float64(hour%12)*30.0 + float64(minutes)*0.5

diff := math.Abs(hourAngle - minuteAngle)
return math.Min(diff, 360.0-diff)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"math"
"testing"
)

func TestAngleClock(t *testing.T) {
const epsilon = 1e-5

tests := []struct {
name string
hour int
minutes int
expected float64
}{
{"example 1: 12:30 -> 165", 12, 30, 165},
{"example 2: 3:30 -> 75", 3, 30, 75},
{"example 3: 3:15 -> 7.5", 3, 15, 7.5},
{"edge case: 12:00 hands overlap -> 0", 12, 0, 0},
{"edge case: 6:00 hands opposite -> 180", 6, 0, 180},
{"edge case: 3:00 right angle -> 90", 3, 0, 90},
{"edge case: 1:00 -> 30", 1, 0, 30},
{"edge case: 12:59 reflex handled -> 35.5", 12, 59, 35.5},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := angleClock(tt.hour, tt.minutes)
if math.Abs(result-tt.expected) > epsilon {
t.Errorf("angleClock(%d, %d) = %v, want %v", tt.hour, tt.minutes, result, tt.expected)
}
})
}
}