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:
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 <= 59Problem Overview: You are given the hour and minutes on an analog clock. The task is to compute the smaller angle formed between the hour hand and the minute hand. The answer must be accurate even when the hour hand has partially moved due to minutes.
Approach 1: Mathematical Calculation of Angle (O(1) time, O(1) space)
An analog clock completes a full circle of 360°. The minute hand moves 360/60 = 6° per minute, while the hour hand moves 360/12 = 30° per hour plus an additional fraction as minutes pass. Compute the minute hand angle using minutes × 6. Compute the hour hand angle using hour × 30 + minutes × 0.5. The absolute difference between these two values gives one angle, but clocks form two possible angles; return the smaller of diff and 360 - diff. This direct formula approach is constant time and relies purely on math reasoning rather than simulation.
Approach 2: Translation of Rotational Movements into Angles (O(1) time, O(1) space)
Instead of memorizing formulas, derive the angles by modeling how each hand rotates across the clock face. The minute hand rotates 6° every minute, so its position is simply minutes × 6. The hour hand rotates 30° every hour and continues moving 0.5° each minute as the hour progresses. Add these two contributions to get the hour hand’s current position. Compute the absolute difference between the two hand angles and normalize the result to the smaller angle using min(diff, 360 - diff). This framing makes the problem intuitive and emphasizes rotational motion, which is common in math and geometry style interview problems.
Recommended for interviews: Interviewers expect the mathematical constant-time solution. It shows you understand how the clock hands move continuously rather than jumping between discrete hours. Explaining the derivation of minute = m × 6 and hour = h × 30 + m × 0.5 demonstrates strong fundamentals in math reasoning and problem modeling.
This approach involves calculating the angles made by the hour hand and the minute hand with respect to 12:00 and then finding the smallest angle between them. The minute hand moves 6 degrees per minute (360 degrees / 60 minutes), and the hour hand moves 30 degrees per hour (360 degrees / 12 hours) plus an additional 0.5 degrees per minute. The difference between these angles gives us the desired result.
The C program computes angles for the hour and minute hands separately. The minute hand covers 6 degrees per minute. The hour hand moves 30 degrees per hour and 0.5 degrees per minute. The difference between these angles is calculated, and the smaller angle is determined by comparing with 180 degrees. The main function demonstrates usage with example times.
Time Complexity: O(1) - The computation involves a fixed number of arithmetic operations.
Space Complexity: O(1) - No additional space is required beyond fixed-size variables.
This alternative approach converts the movement of the clock hands into their equivalent rotations, effectively translating this into angles. The goal is to determine the position of both hands as angles relative to the 12 o'clock position and compute the minimal angular difference.
The C solution adjusts the total time into minutes, facilitating the computation of angles directly based on the total elapsed minutes. This methodology leverages modular arithmetic to find positions relative to whole-hour and half-hour rotations, ultimately returning the minimal calculated angle.
Time Complexity: O(1) - Fixed-time operations based on input.
Space Complexity: O(1) - Uses a constant number of variables.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Mathematical Calculation of Angle | Time Complexity: O(1) - The computation involves a fixed number of arithmetic operations. |
| Translation of Rotational Movements into Angles | Time Complexity: O(1) - Fixed-time operations based on input. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Mathematical Calculation of Angle | O(1) | O(1) | Best general solution. Direct formula gives the answer instantly. |
| Translation of Rotational Movements into Angles | O(1) | O(1) | Useful for understanding the derivation and explaining reasoning in interviews. |
Angle Between Hands of a Clock | LeetCode 1344 | C++, Java, Python • Knowledge Center • 21,610 views views
Watch 9 more video solutions →Practice Angle Between Hands of a Clock with our built-in code editor and test cases.
Practice on FleetCode