Angle Between Hands of a Clock - Problem

Given two integers hour and minutes, return the smaller angle (in degrees) formed between the hour and minute hands on a clock.

The angle should be calculated as a positive floating-point number. Answers within 10^-5 of the actual value will be accepted as correct.

Note: The hour hand moves continuously as minutes pass, not in discrete jumps.

Input & Output

Example 1 — Basic Case
$ Input: hour = 12, minutes = 30
Output: 165.0
💡 Note: At 12:30, minute hand points to 6 (180°), hour hand is halfway between 12 and 1 (15°). Angle = |180° - 15°| = 165°
Example 2 — Perfect Alignment
$ Input: hour = 3, minutes = 0
Output: 90.0
💡 Note: At 3:00, minute hand points to 12 (0°), hour hand points to 3 (90°). Angle = |90° - 0°| = 90°
Example 3 — Obtuse Angle Case
$ Input: hour = 3, minutes = 30
Output: 75.0
💡 Note: At 3:30, minute hand at 180°, hour hand at 105° (3×30° + 30×0.5°). Angle = |180° - 105°| = 75°

Constraints

  • 1 ≤ hour ≤ 12
  • 0 ≤ minutes ≤ 59

Visualization

Tap to expand
Angle Between Hands of a Clock INPUT 12 3 6 9 Hour Minute hour 12 minutes 30 Time: 12:30 Find smaller angle between hands ALGORITHM STEPS 1 Minute Angle 30 * 6 = 180 deg (6 deg per minute) 2 Hour Angle (12%12)*30 + 30*0.5 = 0 + 15 = 15 deg (30 deg/hr + 0.5 deg/min) 3 Calculate Difference |180 - 15| = 165 deg abs(minute - hour) 4 Get Smaller Angle min(165, 360-165) = min(165, 195) = 165 hourAngle = (hour%12)*30 + min*0.5 minAngle = minutes * 6 FINAL RESULT 12 3 6 9 165° Output: 165.0 Smaller angle: OK 165 < 195 (360-165) Time: O(1), Space: O(1) Key Insight: The hour hand moves continuously - it advances 0.5 degrees per minute (30 degrees per hour / 60 minutes). Always return the smaller of the two possible angles: min(angle, 360 - angle). Direct formula gives O(1) solution. TutorialsPoint - Angle Between Hands of a Clock | Direct Formula Optimization
Asked in
Amazon 15 Microsoft 12 Apple 8
28.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen