1344. Angle Between Hands of a Clock - Detailed Explanation
Problem Statement
Description:
You are given two integers representing the hour and the minutes on a 12‑hour analog clock. Your task is to compute the smaller angle (in degrees) between the hour hand and the minute hand.
Key Points:
- The clock is a 12‑hour clock.
- The answer should be the minimum angle between the two hands.
- The output is a floating‑point number with an acceptable error margin.
Example 1:
- Input:
hour = 12
,minutes = 30
- Output:
165.0
- Explanation:
- The minute hand points at 180° (30 minutes × 6° per minute).
- The hour hand is at 15° (since 12 is treated as 0, then (0 \times 30 + 30 \times 0.5 = 15)).
- The absolute difference is (|180 - 15| = 165°).
- The complementary angle (360 - 165 = 195°) is larger, so the answer is 165°.
Example 2:
- Input:
hour = 3
,minutes = 0
- Output:
90.0
- Explanation:
- The minute hand is at 0°.
- The hour hand is at 90° ((3 \times 30 = 90)).
- The absolute difference is 90°, and the complementary angle is 270°; hence the result is 90°.
Intuition and Hints
-
Understanding Hand Movements:
- Minute Hand: Moves 360° in 60 minutes. Hence, it moves at 6° per minute.
- Hour Hand: Moves 360° in 12 hours, but it also moves continuously as minutes pass. Hence, it moves at 30° per hour plus an extra (0.5°) per minute.
-
Deriving the Angles:
- Minute Hand Angle:
[\text{minuteAngle} = \text{minutes} \times 6] - Hour Hand Angle:
[\text{hourAngle} = (\text{hour} \mod 12) \times 30 + \text{minutes} \times 0.5] - Angle Difference:
[\text{diff} = \left| \text{hourAngle} - \text{minuteAngle} \right|] - Minimum Angle:
Since the hands can form an angle on either side, the smaller angle is: [\min(\text{diff}, 360 - \text{diff})]
- Minute Hand Angle:
-
Key Hint:
Use the derived formulas to compute the two angles and then select the smaller one.
Approach
Approach 1: Direct Formula Calculation
Explanation:
-
Compute the Minute Hand Angle:
Multiply the number of minutes by 6. -
Compute the Hour Hand Angle:
First, take the hour modulo 12 (since 12 o’clock is treated as 0). Multiply by 30 and add the contribution from the minutes ((0.5) degrees per minute). -
Calculate the Absolute Difference:
Get the absolute difference between the hour and minute hand angles. -
Determine the Minimum Angle:
Since the clock is circular, the smaller angle is the minimum of the difference and (360) minus the difference.
Python Code (Direct Formula Approach):
Java Code (Direct Formula Approach):
Complexity Analysis
-
Time Complexity: O(1)
All computations (multiplication, addition, absolute value, and min operation) take constant time. -
Space Complexity: O(1)
Only a constant number of variables are used.
Common Pitfalls & Edge Cases
- Edge Cases:
- Hour Value of 12:
Remember that 12 o’clock should be treated as 0 for the hour hand calculation. - Zero Minutes:
When minutes are 0, the minute hand is at 0°, so the answer is simply the hour hand’s angle.
- Hour Value of 12:
- Pitfalls:
- Incorrect Angle Calculation:
Failing to account for the continuous movement of the hour hand (i.e., not adding minutes * 0.5) leads to wrong results. - Not Taking the Complementary Angle:
Always compute both the direct difference and (360) minus that difference to ensure the smaller angle is returned.
- Incorrect Angle Calculation:
Complexity Analysis
-
Time Complexity: O(1)
All computations (multiplication, addition, absolute value, and min operation) take constant time. -
Space Complexity: O(1)
Only a constant number of variables are used.
Common Pitfalls & Edge Cases
- Edge Cases:
- Hour Value of 12:
Remember that 12 o’clock should be treated as 0 for the hour hand calculation. - Zero Minutes:
When minutes are 0, the minute hand is at 0°, so the answer is simply the hour hand’s angle.
- Hour Value of 12:
- Pitfalls:
- Incorrect Angle Calculation:
Failing to account for the continuous movement of the hour hand (i.e., not adding minutes * 0.5) leads to wrong results. - Not Taking the Complementary Angle:
Always compute both the direct difference and (360) minus that difference to ensure the smaller angle is returned.
- Incorrect Angle Calculation:
GET YOUR FREE
Coding Questions Catalog
