मुख्य कंटेंट तक स्किप करें

Angle Between Hands of a Clock

Madhav
EditReport

Description:

Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand.

Example 1:

Input: hour = 12, minutes = 30 Output: 165

Example 2:

Input: hour = 3, minutes = 30 Output: 75

Example 3:

Input: hour = 3, minutes = 15 Output: 7.5


Approaches:

1. Mathematical Approach (Relative Speeds)

To find the angle between the hands, we calculate the exact position (in degrees) of both the minute and hour hands relative to the 12:00 position, and then find the absolute difference between them.

  1. Minute Hand: The minute hand moves 360360^\circ in 6060 minutes. Therefore, it moves at a speed of 66^\circ per minute.
  2. Hour Hand: The hour hand moves 360360^\circ in 1212 hours (720720 minutes). Therefore, it moves at a speed of 0.50.5^\circ per minute. It also shifts based on the current hour.
  3. Find the Difference: Subtract the two angles and take the absolute value.
  4. Find the Smaller Angle: A clock is 360360^\circ. If the difference is greater than 180180^\circ, the smaller angle will be on the other side. Return the minimum of the difference and 360360 minus the difference.
  • Time Complexity: O(1)O(1) because the calculation relies on a few basic arithmetic operations, regardless of the input size.
  • Space Complexity: O(1)O(1) because we only use a few variables to store the angles, requiring constant extra space.

Solutions:

C++

class Solution {
public:
double angleClock(int hour, int minutes) {
double minute_angle = minutes * 6.0;
double hour_angle = (hour % 12) * 30.0 + minutes * 0.5;

double diff = std::abs(hour_angle - minute_angle);

return std::min(diff, 360.0 - diff);
}
};

Java

class Solution {
public double angleClock(int hour, int minutes) {
double minuteAngle = minutes * 6.0;
double hourAngle = (hour % 12) * 30.0 + minutes * 0.5;

double diff = Math.abs(hourAngle - minuteAngle);

return Math.min(diff, 360.0 - diff);
}
}

Python

class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
minute_angle = minutes * 6
hour_angle = (hour % 12) * 30 + minutes * 0.5

diff = abs(hour_angle - minute_angle)

return min(diff, 360 - diff)

JavaScript

/**
* @param {number} hour
* @param {number} minutes
* @return {number}
*/
const angleClock = function(hour, minutes) {
const minuteAngle = minutes * 6;
const hourAngle = (hour % 12) * 30 + minutes * 0.5;

const diff = Math.abs(hourAngle - minuteAngle);

return Math.min(diff, 360 - diff);
};
Telemetry Integration

Completed working through this block? Sync progress to workspace.