Angle between hour and minute hand | Practice | GeeksforGeeks

Calculate the angle between the hour hand and minute hand. 
Note: There can be two angles between hands; we need to print a minimum of two. Also, we need to print the floor of the final result angle. For example, if the final angle is 10.61, we need to print 10.

Example 1:
Input:
H = 9 , M = 0
Output:
90
Explanation:
The minimum angle between hour and minute
hand when the time is 9 is 90 degress.
Example 2:
Input:
H = 3 , M = 30
Output:
75
Explanation:
The minimum angle between hour and minute
hand when the time is 3:30 is 75 degress.

Your Task:
You don't need to read, input, or print anything. Your task is to complete the function getAngle(), which takes 2 Integers H and M as input and returns the answer.

Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ H ≤ 12
0 ≤ M < 60
H and M are Integers
0
harshilrpanchal19982 weeks ago
complete java solution
 
class Solution {   static int getAngle(int H , int M) {       // code here      float a = (float) Math.abs(6*(5*H-M) + 0.5*M);       if (a > 180){           return (int) (360- a);       }       return (int) a;   }};
 
enjoy!!!!!
0
mayank20212 months ago
C++
 
int getAngle(int H , int M) {              if (abs(H*30 - M*6 + M*6/12.0) > 180 )          return 360-abs(H*30 - M*6 + M*6/12.0);       else         return abs(H*30 - M*6 + M*6/12.0);   }
0
tharabhai2 months ago
C++ solution:
 
int getAngle(int H , int M)    {       //hr is the angle covered by hr hand       float hr = ((H*60+M)*0.5);              //mi is the angle covered by minute hand       float mi = (M*6);              //absolute difference b/w them       hr = abs(hr-mi);              //checking which side of the circle is smaller       hr = hr>(360-hr)?360-hr:hr;              //floor of that value as asked for       int ans = floor(hr);              return ans;   }
 
Drop suggestions if any :)
0
taiphanvan24032 months ago
int getAngle(int H , int M) {
       float a=abs(6*(5*H-M)+0.5*M);
       if(a>180) return 360-a;
       return a;
   }
0
ramyan18143 months ago
class Solution { public:   int getAngle(int H , int M) {                float ans=abs(30*H+0.5*M-6*M);              return (int)(ans<=180?ans:360-ans);       
0
hasanshaikh19990
This comment was deleted.
+2
Deyyala Srinivas Kumar8 months ago
Deyyala Srinivas Kumar
spoilerint getAngle(int H , int M) {        // code here        int a= abs(((60*H)-(11*M))/2);        if((((60*H)-(11*M))%2)==0)        return min(a,360-a);        else        return min(a,359-a);    }
0
Aditya Raj Singh Gour8 months ago
Aditya Raj Singh Gour
Example is wrong !For H = 9 and M = 0 correct answer is 90
0
Aayush Kumar9 months ago
Aayush Kumar
Is 12:60 equal to 12:00 or 1:00? There's a problem with this question.
0
Ayush9 months ago
Ayush
How the angle between minute and hour hand at 9:60 which mean 10 is 90 degree, ideally isn't it should have to be 60 degree?
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.