CSInterviewing
Problems
Register
Login
Problems
Register
Login
menu
Clock Angle
math
# Clock Angle ## Difficulty Medium ## Tags `math` ## Problem Statement Given two integers `hour` and `minutes`, calculate the smaller angle (in degrees) formed between the hour and minute hands on an analog clock. Return the angle as a floating-point number. ## Constraints - 1 ≤ hour ≤ 12 - 0 ≤ minutes ≤ 59 - The answer should be in the range [0, 180]. ## Signatures ### Python ```python def clock_angle(hour: int, minutes: int) -> float: pass ``` ### Java ```java public class Solution { public double clock_angle(int hour, int minutes) { return null; } } ``` ### Csharp ```csharp public class Solution { public double clock_angle(int hour, int minutes) { throw new NotImplementedException(); } } ``` ### Cpp ```cpp class Solution { public: double clock_angle(int hour, int minutes) { } }; ``` ### C ```c double clock_angle(int hour, int minutes) { } ``` ### Ruby ```ruby def clock_angle(hour, minutes) end ``` ### Perl ```perl sub clock_angle { my ($self, @args) = @_; } ``` ### Scala ```scala object Solution { def clock_angle(hour: Int, minutes: Int): Double = { } } ``` ### Haskell ```haskell clock_angle :: ... -> ... clock_angle args = undefined ``` ### Clojure ```clojure (defn clock_angle [hour minutes] ) ``` ### Scheme ```scheme (define (clock_angle hour minutes) ) ``` ## Examples ### Example 1 **Input:** ``` hour = 12 minutes = 30 ``` **Output:** ``` 165.0 ``` **Explanation:** At 12:30, the minute hand is at 6 (180°). The hour hand has moved halfway from 12 to 1 (15°). Angle = |180 - 15| = 165°. ### Example 2 **Input:** ``` hour = 3 minutes = 0 ``` **Output:** ``` 90.0 ``` **Explanation:** At 3:00, the minute hand is at 12 (0°), hour hand is at 3 (90°). ### Example 3 **Input:** ``` hour = 3 minutes = 15 ``` **Output:** ``` 7.5 ``` **Explanation:** The minute hand is at 3 (90°). The hour hand is slightly past 3 (97.5°). Difference = 7.5°. ### Example 4 **Input:** ``` hour = 6 minutes = 0 ``` **Output:** ``` 180.0 ``` ## Hints 1. The minute hand moves 6° per minute (360° / 60 minutes). 2. The hour hand moves 30° per hour (360° / 12 hours). 3. The hour hand also moves 0.5° per minute (30° / 60 minutes). 4. Calculate both positions from 12 o'clock. 5. Take the absolute difference and return the minimum of the angle and (360 - angle).
Login required
Please log in to view the solution editor and submit code.
Login
Register