Angle Between Hands of a Clock
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
Video Explanation

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.
- Minute Hand: The minute hand moves in minutes. Therefore, it moves at a speed of per minute.
- Hour Hand: The hour hand moves in hours ( minutes). Therefore, it moves at a speed of per minute. It also shifts based on the current hour.
- Find the Difference: Subtract the two angles and take the absolute value.
- Find the Smaller Angle: A clock is . If the difference is greater than , the smaller angle will be on the other side. Return the minimum of the difference and minus the difference.
- Time Complexity: because the calculation relies on a few basic arithmetic operations, regardless of the input size.
- Space Complexity: because we only use a few variables to store the angles, requiring constant extra space.
Solutions
- C++
- Java
- Python
- JavaScript
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);
}
};
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);
}
}
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)
/**
* @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);
};
Done with this topic? Mark it as complete to track your progress.
Related Practice Problems
Handpicked problems sharing similar algorithmic topic tags
An Alternative Way
Solution for Codeforces 2241D: An Alternative Way, utilizing a prefix sum invariant approach.
Bigrams
Solution for Codeforces 2242A: Bigrams, utilizing a greedy frequency counting approach.
Crimson Triples
Solution for Codeforces 2238B: Crimson Triples, utilizing mathematical observations and a greedy approach.
💬 Discuss this page
Have a question or spot something confusing in "Angle Between Hands of a Clock"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.