Show problems as
Classic problem #1344MediumMath

Angle Between Hands of a Clock

Real-world version: Tide Dial Needle Gap
Tracked in this browser only
Read Angle Between Hands of a Clock on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A brass tide dial in a harbour office has a round face split into 12 equal spans. The spans carry the numbers 1 to 12, going clockwise, and span 12 sits at the top of the face. Two needles turn clockwise above the face. The long needle sweeps the whole face once per hour. The short needle drifts continuously and takes twelve hours to come back around, so it usually rests partway between two spans rather than on one.

The dial is read as two whole numbers. hour_mark runs from 1 to 12 and names the span the short needle has most recently reached. minute_mark runs from 0 to 59 and counts how many marks past the top of the face the long needle has travelled, out of the 60 marks that make up one full sweep. When hour_mark is 12 and minute_mark is 0, both needles point straight up.

The two needles cut the face into two wedges. Return the size in degrees of the tighter wedge as a floating-point value. Return the exact size; do not round it or truncate it.

Examples

Example 1

Input
hour_mark = 6, minute_mark = 20
Output
70.0

The needles cut the face into wedges of 70 and 290 degrees, and the tighter of the two is what comes back.

Example 2

Input
hour_mark = 10, minute_mark = 5
Output
87.5

Here the two wedges are 87.5 and 272.5 degrees, so the reported size is not a whole number of degrees.

Example 3

Input
hour_mark = 12, minute_mark = 0
Output
0.0

Both needles point straight up, so there is no wedge between them at all.

Constraints

  • 1 <= hour_mark <= 12
  • 0 <= minute_mark <= 59
  • Both arguments are integers

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def dial_needle_gap(hour_mark: int, minute_mark: int) -> float:
Java
public double dialNeedleGap(int hourMark, int minuteMark)