LeetCode 1344: Angle Between Hands of a Clock – Java Mathematical Solution Explained

Master Clock Angle Problems with Mathematics, Geometry, Formula Derivation, Dry Run Examples, Complexity Analysis, and Interview Tips in Java

Krishna Shrivastava
1 views
LinkedInGithubX
0
0
LeetCode 1344: Angle Between Hands of a Clock – Java Mathematical Solution Explained
Listen to articleAudio version
Ad
Advertisement

Introduction

LeetCode 1344, Angle Between Hands of a Clock, is a classic mathematics and geometry problem frequently asked in coding interviews.

Unlike many algorithmic problems involving arrays, trees, or dynamic programming, this challenge focuses entirely on understanding how an analog clock works and converting that understanding into a simple mathematical formula.

The goal is to determine the smaller angle formed between the hour hand and the minute hand for a given time.

This problem is an excellent example of how mathematical observation can transform what appears to be a simulation problem into a constant-time solution.

Problem Link - Angle Between Hands of a Clock

Problem Statement

Given:

  1. An integer hour
  2. An integer minutes

Return the smaller angle formed between:

  1. The hour hand
  2. The minute hand

of an analog clock.

The answer should be accurate within 10^-5.

Example 1

Input

hour = 12
minutes = 30

Output

165

Explanation

At 12:30:

  1. Minute hand points at 6
  2. Hour hand lies halfway between 12 and 1

The smaller angle between them is:

165°

Example 2

Input

hour = 3
minutes = 30

Output

75

Example 3

Input

hour = 3
minutes = 15

Output

7.5

Understanding Clock Mathematics

To solve this problem efficiently, we first need to understand how clock hands move.

Minute Hand Movement

A clock contains:

360°

and

60 minutes

Therefore:

360 / 60 = 6°

The minute hand moves:

6° per minute

Formula:

Minute Angle = 6 × minutes

Example:

30 minutes

6 × 30 = 180°

Hour Hand Movement

The clock has:

12 hours

and

360°

Therefore:

360 / 12 = 30°

The hour hand moves:

30° per hour

Formula:

Hour Angle = 30 × hour

However, most beginners miss one important detail.

The Hour Hand Never Stays Still

At 3:30, the hour hand is not exactly at 3.

It moves continuously as minutes pass.

Since:

30° per hour

and

60 minutes per hour

the hour hand moves:

30 / 60 = 0.5°

per minute.

Therefore:

Hour Angle =
(30 × hour) + (0.5 × minutes)

Deriving the Final Formula

Minute hand angle:

6 × minutes

Hour hand angle:

30 × hour + 0.5 × minutes

Difference:

|Hour Angle − Minute Angle|

Substituting:

|(30 × hour + 0.5 × minutes)
− (6 × minutes)|

Simplifying:

|30 × hour − 5.5 × minutes|

This gives one angle.

But clocks always form two angles.

Choosing the Smaller Angle

Suppose:

Difference = 250°

The other angle would be:

360 − 250 = 110°

Since the problem asks for the smaller angle:

Math.min(diff, 360 - diff)

Optimal Java Solution

class Solution {

public double angleClock(int hour, int minutes) {

double angle =
Math.abs((30 * hour) - (5.5 * minutes));

return angle > 180
? 360 - angle
: angle;
}
}

Dry Run

Input

hour = 3
minutes = 15

Hour Hand Angle

30 × 3 = 90
0.5 × 15 = 7.5

Total = 97.5°

Minute Hand Angle

6 × 15 = 90°

Difference

|97.5 - 90|
=
7.5°

Smaller Angle

7.5°

Output:

7.5

Dry Run 2

Input

hour = 12
minutes = 30

Formula

|30 × 12 − 5.5 × 30|

|360 − 165|

195°

Since:

195 > 180

Choose:

360 − 195
=
165°

Output:

165

Why This Solution Is Optimal

Many beginners attempt to:

  1. Simulate clock positions
  2. Create arrays
  3. Use loops

None of these are required.

The entire problem can be solved using a direct mathematical formula.

No iteration is needed.

No additional data structures are needed.

Complexity Analysis

Time Complexity

O(1)

Only a few arithmetic operations are performed.

Space Complexity

O(1)

No extra memory is used.

Common Interview Mistakes

Mistake 1

Ignoring minute movement of the hour hand.

Wrong:

Hour Angle = 30 × hour

Correct:

Hour Angle =
30 × hour + 0.5 × minutes

Mistake 2

Returning the larger angle.

The problem asks for:

Smaller Angle

Always compare:

angle

and

360 - angle

Mistake 3

Forgetting absolute value.

Without:

Math.abs()

negative angles may occur.

Key Takeaways

  1. Minute hand moves 6° every minute.
  2. Hour hand moves 30° every hour.
  3. Hour hand also moves 0.5° every minute.
  4. The formula simplifies to:
|30 × hour − 5.5 × minutes|
  1. Always return the smaller of the two possible angles.
  2. The solution runs in O(1) time and O(1) space.

Conclusion

LeetCode 1344: Angle Between Hands of a Clock is a beautiful mathematical problem that demonstrates how understanding the underlying mechanics of a clock leads to an elegant constant-time solution.

Instead of simulating movement, we directly calculate the positions of both hands using geometry and arithmetic. This results in a clean, interview-friendly solution with optimal performance.

Problems like this highlight an important lesson in programming:

Sometimes the best algorithm is not an algorithm at all—it is mathematics.
Ai Assistant Kas