We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Angle Between Hands of a Clock

Number: 1446

Difficulty: Medium

Paid? No

Companies: Microsoft, Amazon, Epic Systems, Meta, Siemens, Apple


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.


Code Solutions

# Function to calculate the angle between the clock hands
def angleClock(hour, minutes):
    # Calculate the angle of the hour hand (each hour equals 30 degrees plus 0.5 per minute)
    hour_angle = (hour % 12) * 30 + minutes * 0.5
    # Calculate the angle of the minute hand (each minute equals 6 degrees)
    minute_angle = minutes * 6
    # Calculate the absolute difference between the two angles
    angle_diff = abs(hour_angle - minute_angle)
    # Return the smaller angle (either the angle_diff or its supplement)
    return min(angle_diff, 360 - angle_diff)

# Example usage:
print(angleClock(12, 30))  # Expected output: 165
print(angleClock(3, 30))   # Expected output: 75
print(angleClock(3, 15))   # Expected output: 7.5
← Back to All Questions