1344. Angle Between Hands of a Clock
Difficulty: Medium
Topics: Staff, Math, Biweekly Contest 19
Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand.
Answers within 10⁻⁵ of the actual value will be accepted as correct.
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
Constraints:
1 <= hour <= 120 <= minutes <= 59
Hint:
- The tricky part is determining how the minute hand affects the position of the hour hand.
- Calculate the angles separately then find the difference.
Solution:
We implement a direct mathematical solution to compute the smaller angle between the clock hands. By calculating each hand’s angular position independently and then taking the minimum of the absolute difference and its complement to 360°, we efficiently obtain the result in constant time.
Approach
-
Minute hand angle – Each minute moves the minute hand by
6°(since360° / 60 = 6°). -
Hour hand angle – Each hour moves the hour hand by
30°(since360° / 12 = 30°), plus an additional0.5°per minute (since30° / 60 = 0.5°). -
Normalize hour – Convert
12to0because the12‑hourdial wraps around. - Compute difference – Take the absolute angular difference.
-
Return smaller angle – Since two lines form two angles (
θand360°−θ), we return the minimum.
Let's implement this solution in PHP: 1. Angle Between Hands of a Clock
<?php
/**
* @param Integer $hour
* @param Integer $minutes
* @return Float
*/
function angleClock(int $hour, int $minutes): float
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo angleClock(12, 30); // Output: 165
echo angleClock(3, 30); // Output: 75
echo angleClock(3, 15); // Output: 7.5
?>
Explanation:
- The minute hand angle is simply
minutes * 6. - The hour hand angle is
(hour % 12) * 30 + minutes * 0.5, accounting for the hour hand's continuous movement. - The absolute difference
|hourAngle - minuteAngle|gives one of the two angles. - The smaller angle is always
min(diff, 360 - diff)because the total circle is360°. - This works for all valid inputs (
1–12 hour,0–59 minutes) and meets the required precision (10⁻⁵).
Complexity Analysis
- Time Complexity: O(1) – only a few arithmetic operations, independent of input size.
- Space Complexity: O(1) – uses only a constant number of variables.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:



Top comments (0)