Angle Between Hands of a Clock solution and explanation
9 more videos on this problem
Angle Between Hands of a Clock | LeetCode 1344 | C++, Java, PythonKnowledge Center · 8:57 · 22K views
ANGLE BETWEEN HANDS OF A CLOCK | LEETCODE 1344 | PYTHON SOLUTIONCracking FAANG · 10:04 · 3K views
Angle Between Hands of a Clock - LeetCode 1344 PythonDeepti Talesra · 5:13 · 2K views
Angle Between Hands of a Clock 🔥 Leetcode 1344 | Daily Challenge | Optimal Solution + Dry Run Study Placement · 18:55 · 2K views
Angle Between Hands of a Clock | Leet code 1344 | Theory explained + Python codeSai Anish Malla · 6:42 · 1K views
Angle Between Hands of a Clock | LeetCode - 1344Shivam Gupta · 11:15 · 597 views
Angle Between Hands of a Clock (Leetcode 1344)Coding Interviews · 10:35 · 585 views
1344. Angle Between Hands of a Clock | Leetcode Daily - PythonLeetcode Daily · 7:11 · 287 views
Daily Leetcode #2270: Jun 18, 2026 - 1344. Angle Between Hands of a ClockProgramming Live with Larry · 10:23 · 252 views
Leetcode 1344 Angle Between Hands of a ClockRen Zhang · 4:33 · 228 views
The problem
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 <= 120 <= 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 6° 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.
class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
minute_angle = minutes * 6
hour_angle = (hour % 12) * 30 + minutes * 0.5
angle = abs(minute_angle - hour_angle)
return min(angle, 360 - angle)
sol = Solution()
print(f"{sol.angleClock(12, 30):.5f}") # Output: 165.00000
print(f"{sol.angleClock(3, 30):.5f}") # Output: 75.00000
print(f"{sol.angleClock(3, 15):.5f}") # Output: 7.50000public class Solution {
public double angleClock(int hour, int minutes) {
double minute_angle = minutes * 6.0;
double hour_angle = (hour % 12) * 30.0 + minutes * 0.5;
double angle = Math.abs(minute_angle - hour_angle);
return angle > 180 ? 360 - angle : angle;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.printf("%.5f\n", sol.angleClock(12, 30)); // Output: 165.00000
System.out.printf("%.5f\n", sol.angleClock(3, 30)); // Output: 75.00000
System.out.printf("%.5f\n", sol.angleClock(3, 15)); // Output: 7.50000
}
}#include <iostream>
#include <cmath>
class Solution {
public:
double angleClock(int hour, int minutes) {
double minute_angle = minutes * 6;
double hour_angle = (hour % 12) * 30 + minutes * 0.5;
double angle = fabs(minute_angle - hour_angle);
return angle > 180 ? 360 - angle : angle;
}
};
int main() {
Solution sol;
std::cout << std::fixed << sol.angleClock(12, 30) << std::endl; // Output: 165.00000
std::cout << std::fixed << sol.angleClock(3, 30) << std::endl; // Output: 75.00000
std::cout << std::fixed << sol.angleClock(3, 15) << std::endl; // Output: 7.50000
return 0;
}class Solution {
angleClock(hour, minutes) {
let minute_angle = minutes * 6;
let hour_angle = (hour % 12) * 30 + minutes * 0.5;
let angle = Math.abs(minute_angle - hour_angle);
return angle > 180 ? 360 - angle : angle;
}
}
const sol = new Solution();
console.log(sol.angleClock(12, 30).toFixed(5)); // Output: 165.00000
console.log(sol.angleClock(3, 30).toFixed(5)); // Output: 75.00000
console.log(sol.angleClock(3, 15).toFixed(5)); // Output: 7.50000#include <stdio.h>
#include <math.h>
float angleClock(int hour, int minutes) {
double minute_angle = minutes * 6.0;
double hour_angle = (hour % 12) * 30.0 + minutes * 0.5;
double angle = fabs(minute_angle - hour_angle);
return (angle > 180) ? 360 - angle : angle;
}
int main() {
printf("%.5f\n", angleClock(12, 30)); // Output: 165.00000
printf("%.5f\n", angleClock(3, 30)); // Output: 75.00000
printf("%.5f\n", angleClock(3, 15)); // Output: 7.50000
return 0;
}using System;
public class Solution {
public double AngleClock(int hour, int minutes) {
double minute_angle = minutes * 6;
double hour_angle = (hour % 12) * 30 + minutes * 0.5;
double angle = Math.Abs(minute_angle - hour_angle);
return angle > 180 ? 360 - angle : angle;
}
public static void Main() {
Solution sol = new Solution();
Console.WriteLine(sol.AngleClock(12, 30).ToString("0.00000")); // Output: 165.00000
Console.WriteLine(sol.AngleClock(3, 30).ToString("0.00000")); // Output: 75.00000
Console.WriteLine(sol.AngleClock(3, 15).ToString("0.00000")); // Output: 7.50000
}
}Python solution uses the same arithmetic logic to calculate the angles and returns the minimum of the calculated angle and its complement (360 minus the angle). The formatted string ensures precision in printing the result.
Time Complexity: O(1) - Constant operations on input values. Space Complexity: O(1) - Fixed amount of space usage.
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.
class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
total_minutes = hour * 60 + minutes
minute_angle = (total_minutes % 60) * 6
hour_angle = (total_minutes % 720) * 0.5
angle = abs(minute_angle - hour_angle)
return min(angle, 360 - angle)
sol = Solution()
print(f"{sol.angleClock(12, 30):.5f}") # Output: 165.00000
print(f"{sol.angleClock(3, 30):.5f}") # Output: 75.00000
print(f"{sol.angleClock(3, 15):.5f}") # Output: 7.50000public class Solution {
public double angleClock(int hour, int minutes) {
int totalMinutes = hour * 60 + minutes;
double minuteAngle = (totalMinutes % 60) * 6.0;
double hourAngle = (totalMinutes % 720) * 0.5;
double angle = Math.abs(minuteAngle - hourAngle);
return angle > 180 ? 360 - angle : angle;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.printf("%.5f\n", sol.angleClock(12, 30)); // Output: 165.00000
System.out.printf("%.5f\n", sol.angleClock(3, 30)); // Output: 75.00000
System.out.printf("%.5f\n", sol.angleClock(3, 15)); // Output: 7.50000
}
}#include <iostream>
#include <cmath>
class Solution {
public:
double angleClock(int hour, int minutes) {
int total_minutes = hour * 60 + minutes;
double minute_angle = (total_minutes % 60) * 6;
double hour_angle = (total_minutes % 720) * 0.5;
double angle = fabs(minute_angle - hour_angle);
return angle > 180 ? 360 - angle : angle;
}
};
int main() {
Solution sol;
std::cout << std::fixed << sol.angleClock(12, 30) << std::endl; // Output: 165.00000
std::cout << std::fixed << sol.angleClock(3, 30) << std::endl; // Output: 75.00000
std::cout << std::fixed << sol.angleClock(3, 15) << std::endl; // Output: 7.50000
return 0;
}class Solution {
angleClock(hour, minutes) {
let totalMinutes = hour * 60 + minutes;
let minuteAngle = (totalMinutes % 60) * 6;
let hourAngle = (totalMinutes % 720) * 0.5;
let angle = Math.abs(minuteAngle - hourAngle);
return angle > 180 ? 360 - angle : angle;
}
}
const sol = new Solution();
console.log(sol.angleClock(12, 30).toFixed(5)); // Output: 165.00000
console.log(sol.angleClock(3, 30).toFixed(5)); // Output: 75.00000
console.log(sol.angleClock(3, 15).toFixed(5)); // Output: 7.50000#include <stdio.h>
#include <math.h>
float angleClock(int hour, int minutes) {
int total_minutes = hour * 60 + minutes;
double minute_angle = (total_minutes % 60) * 6.0;
double hour_angle = (total_minutes % 720) * 0.5;
double angle = fabs(minute_angle - hour_angle);
return (angle > 180) ? 360 - angle : angle;
}
int main() {
printf("%.5f\n", angleClock(12, 30)); // Output: 165.00000
printf("%.5f\n", angleClock(3, 30)); // Output: 75.00000
printf("%.5f\n", angleClock(3, 15)); // Output: 7.50000
return 0;
}using System;
public class Solution {
public double AngleClock(int hour, int minutes) {
int totalMinutes = hour * 60 + minutes;
double minuteAngle = (totalMinutes % 60) * 6;
double hourAngle = (totalMinutes % 720) * 0.5;
double angle = Math.Abs(minuteAngle - hourAngle);
return angle > 180 ? 360 - angle : angle;
}
public static void Main() {
Solution sol = new Solution();
Console.WriteLine(sol.AngleClock(12, 30).ToString("0.00000")); // Output: 165.00000
Console.WriteLine(sol.AngleClock(3, 30).ToString("0.00000")); // Output: 75.00000
Console.WriteLine(sol.AngleClock(3, 15).ToString("0.00000")); // Output: 7.50000
}
}Python's version condenses tasks similarly, utilizing the totality of elapsed minutes to determine rotational alignments for both clock hands, generating the resultant minimal deviation in their absolute displacement.
Time Complexity: O(1) - Invariable calculations simplicity. Space Complexity: O(1) - Direct and limited space consumption.
Approach 3: Default Approach
function angleClock(hour: number, minutes: number): number {
const h = 30 * hour + 0.5 * minutes;
const m = 6 * minutes;
const diff = Math.abs(h - m);
return Math.min(diff, 360 - diff);
}func angleClock(hour int, minutes int) float64 {
h := 30*float64(hour) + 0.5*float64(minutes)
m := 6 * float64(minutes)
diff := math.Abs(h - m)
return math.Min(diff, 360-diff)
}Complexity comparison
| Approach | Complexity |
|---|---|
| Mathematical Calculation of Angle | Time Complexity: O(1) - Constant operations on input values. Space Complexity: O(1) - Fixed amount of space usage. |
| Translation of Rotational Movements into Angles | Time Complexity: O(1) - Invariable calculations simplicity. Space Complexity: O(1) - Direct and limited space consumption. |
| Default Approach | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Mathematical Calculation of Angle | O(1) | O(1) | Best general solution. Direct formula gives the answer instantly. |
| Translation of Rotational Movements into Angles | O(1) | O(1) | Useful for understanding the derivation and explaining reasoning in interviews. |
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).
