[Medium] 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 of a clock.
Examples
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 <= 120 <= minutes <= 59
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Brute force (this problem) | Often O(n^2) or O(2^n) | O(n) | Baseline; clarifies the optimization target |
| Sort + scan | O(n log n) | O(1) | Pairs, intervals, greedy ordering |
| Hash map / set | O(n) | O(n) | Frequency, membership, two-sum style |
| Single-pass linear | O(n) | O(1) | Two pointers, sliding window, Kadane |
Thinking Process
A clock face is a circle of 360 degrees. We need to compute each hand’s angle independently, then find the smaller of the two possible angles between them.
Hour Hand Angle
The hour hand moves based on both the hour and the minutes:
- Each hour mark =
360 / 12 = 30degrees - The minute component shifts the hour hand by
minutes / 60of one hour mark
$text{hourAngle} = (text{hour} bmod 12 + text{minutes} / 60.0) × 30.0
Minute Hand Angle
The minute hand moves based only on minutes:
- Each minute mark =
360 / 60 = 6degrees
text{minuteAngle} = (text{minutes} bmod 60) × 6.0
The Smaller Angle
The raw difference might be the “long way around” the clock. The answer is the smaller of the two arcs:
text{answer} = min(text{diff},\ 360 - text{diff})
Walk-Through: hour=12, minutes=30
hourAngle = (12 % 12 + 30/60.0) * 30.0 = (0 + 0.5) * 30 = 15°
minuteAngle = (30 % 60) * 6.0 = 180°
diff = |15 - 180| = 165°
answer = min(165, 360 - 165) = min(165, 195) = 165°
Approach: Direct Calculation – O(1)$
class Solution {
public:
double angleClock(int hour, int minutes) {
double hourAngle = (hour % 12 + minutes / 60.0) * 30.0;
double minuteAngle = (minutes % 60) * 6.0;
double diff = abs(hourAngle - minuteAngle);
return min(diff, 360.0 - diff);
}
};
Solution Explanation
Approach: Brute force (this problem)
Key idea: A clock face is a circle of 360 degrees. We need to compute each hand’s angle independently, then find the smaller of the two possible angles between them.
How the code works:
- Each hour mark =
360 / 12 = 30degrees - The minute component shifts the hour hand by
minutes / 60of one hour mark - Each minute mark =
360 / 60 = 6degrees
Walkthrough — input hour = 12, minutes = 30, expected output 165:
- Initialize variables from the problem setup.
- Apply the main loop / recursion until the condition is met.
- Confirm the result matches the expected output.
Common Mistakes
- Forgetting that the hour hand also moves with minutes (not just snapping to hour marks)
- Not handling
hour = 12– must usehour % 12to map 12 to 0 - Using integer division for
minutes / 60instead ofminutes / 60.0(loses the fractional shift) - Returning the raw difference without considering the shorter arc (
min(diff, 360 - diff))
Key Takeaways
- Break the problem into independent sub-computations (each hand’s angle), then combine
- The
min(diff, 360 - diff)pattern applies to any “shorter arc on a circle” problem - Use floating-point division (
60.0) to preserve the fractional hour-hand movement
Related Problems
- 2515. Shortest Distance to Target String in a Circular Array – circular distance pattern
References
- LC 1344: Angle Between Hands of a Clock on LeetCode
- LeetCode Discuss — LC 1344: Angle Between Hands of a Clock
- LeetCode Editorial (may require premium)