https://github.com/emahtab/angle-between-hands-of-a-clock
https://github.com/emahtab/angle-between-hands-of-a-clock
clock leetcode maths
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/angle-between-hands-of-a-clock
- Owner: eMahtab
- Created: 2021-09-25T13:43:35.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-09-25T13:51:30.000Z (about 4 years ago)
- Last Synced: 2025-02-02T03:26:21.945Z (8 months ago)
- Topics: clock, leetcode, maths
- Homepage:
- Size: 2.93 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Angle Between Hands of a clock
## https://leetcode.com/problems/angle-between-hands-of-a-clockGiven two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand.
```
Constraints:1. 1 <= hour <= 12
2. 0 <= minutes <= 59
3. Answers within 10^-5 of the actual value will be accepted as correct.
``````java
class Solution {
public double angleClock(int hour, int minutes) {
// In one minute, minute's hand makes 6 degree
// In one minute, hour's hand makes 1/2 = 0.5 degree
double hoursHand = ((hour % 12) * 60 + minutes) / 2.0; // Caution : divide by 2.0 not 2
int minutesHand = minutes * 6;
double angle = Math.abs(hoursHand-minutesHand);
if(angle <= 180)
return angle;
// We have to return the smaller angle
double min = Math.min(hoursHand, minutesHand);
double max = Math.max(hoursHand, minutesHand);
angle = (360 - max) + min;
return angle;
}
}
```