Skip to main content

Angle Between Hands of a Clock - Solution & Explanation

MediumMath13 min readAsked at: Amazon, Microsoft, Meta +7
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: You are given the hour and minutes on an analog clock. The task is to compute the smaller angle formed between the hour hand and the minute hand. The answer must be accurate even when the hour hand has partially moved due to minutes.

Approach 1: Mathematical Calculation of Angle (O(1) time, O(1) space)

An analog clock completes a full circle of 360°. The minute hand moves 360/60 = 6° per minute, while the hour hand moves 360/12 = 30° per hour plus an additional fraction as minutes pass. Compute the minute hand angle using minutes × 6. Compute the hour hand angle using hour × 30 + minutes × 0.5. The absolute difference between these two values gives one angle, but clocks form two possible angles; return the smaller of diff and 360 - diff. This direct formula approach is constant time and relies purely on math reasoning rather than simulation.

Approach 2: Translation of Rotational Movements into Angles (O(1) time, O(1) space)

Instead of memorizing formulas, derive the angles by modeling how each hand rotates across the clock face. The minute hand rotates every minute, so its position is simply minutes × 6. The hour hand rotates 30° every hour and continues moving 0.5° each minute as the hour progresses. Add these two contributions to get the hour hand’s current position. Compute the absolute difference between the two hand angles and normalize the result to the smaller angle using min(diff, 360 - diff). This framing makes the problem intuitive and emphasizes rotational motion, which is common in math and geometry style interview problems.

Recommended for interviews: Interviewers expect the mathematical constant-time solution. It shows you understand how the clock hands move continuously rather than jumping between discrete hours. Explaining the derivation of minute = m × 6 and hour = h × 30 + m × 0.5 demonstrates strong fundamentals in math reasoning and problem modeling.

Approach 1: Mathematical Calculation of Angle

This approach involves calculating the angles made by the hour hand and the minute hand with respect to 12:00 and then finding the smallest angle between them. The minute hand moves 6 degrees per minute (360 degrees / 60 minutes), and the hour hand moves 30 degrees per hour (360 degrees / 12 hours) plus an additional 0.5 degrees per minute. The difference between these angles gives us the desired result.

The C program computes angles for the hour and minute hands separately. The minute hand covers 6 degrees per minute. The hour hand moves 30 degrees per hour and 0.5 degrees per minute. The difference between these angles is calculated, and the smaller angle is determined by comparing with 180 degrees. The main function demonstrates usage with example times.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) - The computation involves a fixed number of arithmetic operations.
Space Complexity: O(1) - No additional space is required beyond fixed-size variables.

Try this approach in the editor →

Approach 2: Translation of Rotational Movements into Angles

This alternative approach converts the movement of the clock hands into their equivalent rotations, effectively translating this into angles. The goal is to determine the position of both hands as angles relative to the 12 o'clock position and compute the minimal angular difference.

The C solution adjusts the total time into minutes, facilitating the computation of angles directly based on the total elapsed minutes. This methodology leverages modular arithmetic to find positions relative to whole-hour and half-hour rotations, ultimately returning the minimal calculated angle.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) - Fixed-time operations based on input.
Space Complexity: O(1) - Uses a constant number of variables.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Mathematical Calculation of Angle

Time Complexity: O(1) - The computation involves a fixed number of arithmetic operations.
Space Complexity: O(1) - No additional space is required beyond fixed-size variables.

Translation of Rotational Movements into Angles

Time Complexity: O(1) - Fixed-time operations based on input.
Space Complexity: O(1) - Uses a constant number of variables.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Mathematical Calculation of AngleO(1)O(1)Best general solution. Direct formula gives the answer instantly.
Translation of Rotational Movements into AnglesO(1)O(1)Useful for understanding the derivation and explaining reasoning in interviews.

Video Solution

Angle Between Hands of a Clock | LeetCode 1344 | C++, Java, PythonKnowledge Center22,411 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Angle Between Hands of a Clock easy or hard?
The problem is rated Medium on LeetCode but conceptually closer to an easy math problem once you recognize how the clock hands move. The challenge is remembering that the hour hand moves continuously as minutes pass.
Angle Between Hands of a Clock Python/Java solution
The implementation is identical across languages. Calculate minuteAngle = minutes * 6 and hourAngle = hour * 30 + minutes * 0.5, compute the absolute difference, and return min(diff, 360 - diff). This logic works the same in Python, Java, C++, C#, and JavaScript.
How to solve Angle Between Hands of a Clock in O(n)?
The problem does not require an O(n) algorithm. The angle can be computed directly using constant-time math formulas derived from the rotational movement of the clock hands. The final computation uses only a few arithmetic operations, giving O(1) time complexity.
What is the best approach for Angle Between Hands of a Clock?
The best approach is a direct mathematical calculation. Compute the minute hand angle as minutes × 6 and the hour hand angle as hour × 30 + minutes × 0.5. Take the absolute difference and return the smaller value between diff and 360 − diff. This runs in O(1) time and O(1) space.
Is Angle Between Hands of a Clock asked at Google/Amazon/Meta?
Clock-angle problems appear frequently in technical interviews at companies like Google and Amazon as quick math or reasoning checks. They test whether candidates can model real-world movement with formulas and handle edge cases like partial hour movement.
What data structure is used in Angle Between Hands of a Clock?
No special data structure is required. The problem is purely a mathematical computation involving arithmetic operations and angle normalization.
What is the time complexity of Angle Between Hands of a Clock?
The optimal solution runs in O(1) time because it uses a constant number of arithmetic operations. No loops, recursion, or additional data structures are required. Space complexity is also O(1).

Ready to solve this problem?

Practice Angle Between Hands of a Clock with our built-in code editor and test cases.

Practice on FleetCode