1344. Angle Between Hands of a Clock LeetCode Solution

In this guide, you will get 1344. Angle Between Hands of a Clock LeetCode Solution with the best time and space complexity. The solution to Angle Between Hands of a Clock problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Angle Between Hands of a Clock solution in C++
  4. Angle Between Hands of a Clock solution in Java
  5. Angle Between Hands of a Clock solution in Python
  6. Additional Resources
1344. Angle Between Hands of a Clock LeetCode Solution image

Problem Statement of Angle Between Hands of a Clock

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.

Example 1:

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

Example 2:

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

Example 3:

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

Constraints:

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

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

1344. Angle Between Hands of a Clock LeetCode Solution in C++

class Solution {
 public:
  double angleClock(int hour, int minutes) {
    const double hourHand = (hour % 12 + minutes / 60.0) * 30;
    const double minuteHand = minutes * 6;
    const double diff = abs(hourHand - minuteHand);
    return min(diff, 360 - diff);
  }
};
/* code provided by PROGIEZ */

1344. Angle Between Hands of a Clock LeetCode Solution in Java

class Solution {
  public double angleClock(int hour, int minutes) {
    final double hourHand = (hour % 12 + minutes / 60.0) * 30;
    final double minuteHand = minutes * 6;
    final double diff = Math.abs(hourHand - minuteHand);
    return Math.min(diff, 360 - diff);
  }
}
// code provided by PROGIEZ

1344. Angle Between Hands of a Clock LeetCode Solution in Python

class Solution:
  def angleClock(self, hour: int, minutes: int) -> float:
    hourAngle = (hour % 12) * 30 + minutes * 0.5
    minuteAngle = minutes * 6
    ans = abs(hourAngle - minuteAngle)

    return min(ans, 360 - ans)
# code by PROGIEZ

Additional Resources

See also  1442. Count Triplets That Can Form Two Arrays of Equal XOR LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.