VOOZH about

URL: https://www.geeksforgeeks.org/dsa/calculate-angle-hour-hand-minute-hand/

⇱ Calculate the angle between hour hand and minute hand - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Calculate the angle between hour hand and minute hand

Last Updated : 3 Sep, 2025

Given a string s represents time in 24-hour format ("HH:MM"), determine the minimum angle between the hour and minute hands of an analog clock.

Examples:

Input: s = "06:00"
Output: 180.000
Explanation: When the time is 06:00, the angle between the hour and minute hands of the clock is 180.000 degrees.

Input: s = "03:15"
Output: 7.500
Explanation: When the time is 03:15, the angle between the hour and minute hands of the clock is 7.500 degrees.

Input: s = "00:00"
Output: 0.000
Explanation: When the time is 00:00, the angle between the hour and minute hands of the clock is 0.000 degrees.

[Approach] Using Mathematical Formula - O(1) in Time and O(1) in Space

The minute hand moves 6° per minute, while the hour hand moves 0.5° per minute. Thus, the hour hand's angle is calculated as hrAngle = 30 × H + 0.5 × M, and the minute hand's angle as minAngle = 6 × M. The difference between the two angles is diff = |hrAngle - minAngle|.

Since the clock follows a 12-hour format, any 24-hour input should be converted using H = H % 12. After finding the absolute difference between the two angles, the smaller angle is determined using min(diff, 360 - diff).


Output
180.000
Comment