Problem Description
Given two numbers, hour and minutes, determine the smaller angle (in degrees) formed between the hour and the minute hand of a clock. The answer must be accurate within 10^-5 of the actual value.
Key Insights
- The minute hand moves 6 degrees per minute (360 / 60).
- The hour hand moves 30 degrees per hour (360 / 12) plus an additional 0.5 degrees per minute.
- Compute the absolute difference between the hour hand and minute hand angles.
- The smaller angle is the minimum between the computed difference and its supplementary angle (360 minus the difference).
Space and Time Complexity
Time Complexity: O(1)
Space Complexity: O(1)
Solution
We calculate the angle for the hour hand by adding the base angle determined by the hour and the extra movement caused by the minutes. Similarly, the minute hand's angle is computed based on the minutes passed. The absolute difference between these two angles gives one possible angle between the hands. Since a clock is circular, if the computed difference is greater than 180 degrees, we take its complement (360 minus the difference) to obtain the smaller angle. This direct arithmetic method leads to a constant time and space solution.