Problem

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

Answers within 10^-5 of the actual value will be accepted as correct.

Examples

Example 1:

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

Example 2:

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

Example 3:

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

Constraints:

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

Solution

Method 1 – Math Simulation

Intuition

The hour and minute hands move at different rates. By calculating their positions in degrees, we can find the absolute difference and ensure we return the smaller angle (≤180°). For example, at 3:15, the hour hand is at 97.5° and the minute hand at 90°, so the angle is 7.5°.

Approach

  1. Calculate the position of the hour hand:
  • Each hour is 30° (360°/12).
  • The hour hand also moves as minutes pass: each minute adds 0.5° (30°/60).
  • Formula: hour_angle = (hour % 12) * 30 + minutes * 0.5
  1. Calculate the position of the minute hand:
  • Each minute is 6° (360°/60).
  • Formula: minute_angle = minutes * 6
  1. Find the absolute difference between the two angles.
  2. Return the smaller angle (if >180°, subtract from 360°).

Code

1
2
3
4
5
6
7
8
9
class Solution {
public:
   double angleClock(int hour, int minutes) {
      double ha = (hour % 12) * 30 + minutes * 0.5;
      double ma = minutes * 6;
      double ans = abs(ha - ma);
      return ans > 180 ? 360 - ans : ans;
   }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
type Solution struct{}

func (Solution) AngleClock(hour int, minutes int) float64 {
   ha := float64(hour%12)*30 + float64(minutes)*0.5
   ma := float64(minutes) * 6
   ans := ha - ma
   if ans < 0 {
      ans = -ans
   }
   if ans > 180 {
      return 360 - ans
   }
   return ans
}
1
2
3
4
5
6
7
8
class Solution {
   public double angleClock(int hour, int minutes) {
      double ha = (hour % 12) * 30 + minutes * 0.5;
      double ma = minutes * 6;
      double ans = Math.abs(ha - ma);
      return ans > 180 ? 360 - ans : ans;
   }
}
1
2
3
4
5
6
7
8
class Solution {
   fun angleClock(hour: Int, minutes: Int): Double {
      val ha = (hour % 12) * 30 + minutes * 0.5
      val ma = minutes * 6.0
      val ans = kotlin.math.abs(ha - ma)
      return if (ans > 180) 360 - ans else ans
   }
}
1
2
3
4
5
6
class Solution:
   def angleClock(self, hour: int, minutes: int) -> float:
      ha: float = (hour % 12) * 30 + minutes * 0.5
      ma: float = minutes * 6
      ans: float = abs(ha - ma)
      return 360 - ans if ans > 180 else ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
impl Solution {
   pub fn angle_clock(hour: i32, minutes: i32) -> f64 {
      let ha = (hour % 12) as f64 * 30.0 + minutes as f64 * 0.5;
      let ma = minutes as f64 * 6.0;
      let mut ans = (ha - ma).abs();
      if ans > 180.0 {
        ans = 360.0 - ans;
      }
      ans
   }
}

Complexity

  • ⏰ Time complexity: O(1) (constant time arithmetic)
  • 🧺 Space complexity: O(1) (no extra space)