LeetCode Problem Workspace

Angle Between Hands of a Clock

Calculate the smaller angle between the hour and minute hands of a clock based on given time inputs.

category

1

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Medium · Math-driven solution strategy

bolt

Answer-first summary

Calculate the smaller angle between the hour and minute hands of a clock based on given time inputs.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math-driven solution strategy

Try AiBox Copilotarrow_forward

This problem requires calculating the smaller angle between the hour and minute hands of a clock based on given hour and minute values. Focus on understanding how the position of the minute hand impacts the hour hand's position and the overall angle. The solution should efficiently compute the angle based on simple math formulas related to the clock's mechanics.

Problem Statement

Given an hour (1 to 12) and minutes (0 to 59), calculate the smaller angle in degrees between the hour and minute hands of a clock. The clock hands are connected by a 360-degree circle, where both hands are positioned based on the time provided. The problem requires identifying the precise smaller angle between the two hands at any given time.

The minute hand moves 6 degrees per minute, while the hour hand moves 0.5 degrees per minute. The challenge is to compute the angle formed between the two hands considering the minute hand's effect on the hour hand's position. An answer within a 10^-5 margin of error from the correct result is accepted.

Examples

Example 1

Input: hour = 12, minutes = 30

Output: 165

Example details omitted.

Example 2

Input: hour = 3, minutes = 30

Output: 75

Example details omitted.

Example 3

Input: hour = 3, minutes = 15

Output: 7.5

Example details omitted.

Constraints

  • 1 <= hour <= 12
  • 0 <= minutes <= 59

Solution Approach

Understanding the Mechanics

Start by calculating the positions of both hands in terms of degrees. The minute hand moves 6 degrees per minute (360 degrees divided by 60 minutes). The hour hand moves 0.5 degrees per minute (360 degrees divided by 12 hours). For a given time, compute the angles of both hands and find their difference.

Adjustment for the Smaller Angle

Since the clock's hands form a 360-degree circle, the angle between them can be computed as the absolute difference. After that, check if the angle is more than 180 degrees, in which case subtract it from 360 to ensure the smaller angle is returned.

Handle Minute Impact on Hour Hand

The tricky part is how the minute hand impacts the position of the hour hand. As the minute hand moves, it slightly shifts the hour hand's position. Ensure you account for this minute-based shift in the hour hand's position while computing the final angle.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity depends on the approach used, but typically, it would be O(1) since only a few simple calculations are involved. The space complexity is also O(1) as no additional space is required beyond the variables for hour, minute, and the computed angles.

What Interviewers Usually Probe

  • Pay attention to how the minute hand shifts the hour hand slightly as time progresses.
  • Candidates should show awareness of the fact that the angle must be adjusted for the smaller value, ensuring the correct result is found.
  • Look for clarity in the explanation of the mechanics of both hands and how their positions affect the angle.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to account for the minute hand's effect on the hour hand.
  • Not adjusting the angle when it exceeds 180 degrees to ensure the smaller angle is returned.
  • Misunderstanding how to convert time into angles, leading to incorrect calculations.

Follow-up variants

  • Return the larger angle instead of the smaller one.
  • Calculate the angle between the hands at a different scale, like a 24-hour format clock.
  • Consider a case where the input time is provided in 24-hour format and adjust the formula accordingly.

FAQ

What is the formula for calculating the angle between the clock hands?

To calculate the angle, compute the absolute difference between the hour and minute hand positions. If the difference exceeds 180 degrees, subtract it from 360 to get the smaller angle.

How do I handle the minute hand affecting the hour hand's position?

The hour hand moves by 0.5 degrees per minute, so for each minute, the hour hand shifts slightly. Include this shift when calculating the angle.

Can this problem be solved using a brute force approach?

A brute force approach would be inefficient, as you would need to simulate the movement of the hands for every minute. A math-driven solution is more optimal.

What are the time and space complexities for this problem?

Both time and space complexity are O(1), as only a few basic calculations are required, with no additional memory usage.

What happens if the input time is outside the valid range?

The problem statement guarantees that inputs will be within the valid range (1 <= hour <= 12 and 0 <= minutes <= 59), so no special handling for out-of-range values is needed.

terminal

Solution

Solution 1

#### Python3

1
2
3
4
5
6
class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
        h = 30 * hour + 0.5 * minutes
        m = 6 * minutes
        diff = abs(h - m)
        return min(diff, 360 - diff)
Angle Between Hands of a Clock Solution: Math-driven solution strategy | LeetCode #1344 Medium