Suppose we have two numbers, hour and minutes. We have to find a smaller angle (in sexagesimal units) formed between the hour and the minute hand. So if the input is like hour = 12 and min := 30, then the result will be 165°.
To solve this, we will follow these steps −
if h = 12, then set h := 0
if m = 60, then set m := 0
hAngle := 0.5 * (60h) + m
mAngle := 6m
ret := |hAngle - mAngle|
return minimum of ret and (360 – ret)
Example (C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
double angleClock(int h, int m) {
if(h == 12) h = 0;
if(m == 60) m = 0;
double hAngle = 0.5*((60 * h) + m);
double mAngle = 6 * m;
double ret = abs(hAngle - mAngle);
return min(360 - ret, ret);
}
};
main(){
Solution ob;
cout << (ob.angleClock(12, 30));
}Input
12 30
Output
165.00000