Search⌘ K
AI Features

Angle Between the Hands of a Clock

Explore how to compute the smaller angle between the hour and minute hands of a clock by breaking down their positions into degrees. Understand the step-by-step approach to calculate angles for each hand based on hours and minutes, then find the minimum difference with an efficient algorithm that runs in constant time and space.

Description

Given two numbers, hour and minutes we will return the smaller angle (in degrees) formed between the hour and the minute hands on a clock.

Let’s look at some examples to better understand this:

Coding exercise

Swift
import Swift
func clockAngle(hour: Int, minutes: Int) -> Float64 {
// Your code goes here
return 0.1 // Change the return value as per your requirement
}
Angle between the hands of a clock exercise

Solution

The idea is to separately calculate the angles between the 0-minutes vertical line and each hand. The answer will be the difference that is found between the two angles.

Note: Black lines in the clock represent the 0-minutes vertical lines.

Minute hand angle

Let’s start from the minute hand. The minute hand moves with 1 min intervals. At 1 min, the angle of the minute hand is . . Since the whole circle of the clock is equal to 360° degrees and 60 minutes, we estimated that the minute hand moves 1 min = 360° / 60 = 6° degree with each minute.

Now, we can easily find an angle between the 0-minutes vertical line and a minute hand, using minutesAngle = minutes X 6°. ...