Angle Between Hands of a Clock

medium math

Problem

Given two numbers hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hands of a clock.

Inputhour = 12, minutes = 30
Output165
Minute hand at 180°, hour hand at 15° (the hour hand also moves with minutes). |180 − 15| = 165°.

def angle_clock(hour, minutes):
    h = (hour % 12) * 30 + minutes * 0.5
    m = minutes * 6
    diff = abs(h - m)
    return min(diff, 360 - diff)
function angleClock(hour, minutes) {
  const h = (hour % 12) * 30 + minutes * 0.5;
  const m = minutes * 6;
  const diff = Math.abs(h - m);
  return Math.min(diff, 360 - diff);
}
class Solution {
    public double angleClock(int hour, int minutes) {
        double h = (hour % 12) * 30.0 + minutes * 0.5;
        double m = minutes * 6.0;
        double diff = Math.abs(h - m);
        return Math.min(diff, 360 - diff);
    }
}
double angleClock(int hour, int minutes) {
    double h = (hour % 12) * 30.0 + minutes * 0.5;
    double m = minutes * 6.0;
    double diff = fabs(h - m);
    return min(diff, 360.0 - diff);
}
Time: O(1) Space: O(1)