Calculate the angle between hour hand and minute hand (original) (raw)

Last Updated : 3 Sep, 2025

Given a string s represents time in 24-hour format ("HH:MM"), determine the minimum angle between the hour and minute hands of an analog clock.

**Examples:

**Input: s = "06:00"
**Output: 180.000
**Explanation: When the time is 06:00, the angle between the hour and minute hands of the clock is 180.000 degrees.

**Input: s = "03:15"
**Output: 7.500
**Explanation: When the time is 03:15, the angle between the hour and minute hands of the clock is 7.500 degrees.

**Input: s = "00:00"
**Output: 0.000
**Explanation: When the time is 00:00, the angle between the hour and minute hands of the clock is 0.000 degrees.

Try It Yourselfredirect icon

[Approach] Using Mathematical Formula - O(1) in Time and O(1) in Space

The minute hand moves **6° per minute, while the hour hand moves **0.5° per minute. Thus, the hour hand's angle is calculated as hrAngle = 30 × H + 0.5 × M, and the minute hand's angle as minAngle = 6 × M. The difference between the two angles is diff= |hrAngle - minAngle|.

Since the clock follows a 12-hour format, any 24-hour input should be converted using H = H % 12. After finding the absolute difference between the two angles, the smaller angle is determined using min(diff, 360 - diff).

C++ `

#include #include #include using namespace std;

// Utility function to return the minimum of two double values double getMin(double x, double y) { return (x < y) ? x : y; }

// Function to calculate the minimum angle between // hour and minute hands double getAngle(string s) { // Extract hours and minutes from "HH:MM" int h = stoi(s.substr(0, 2)); int m = stoi(s.substr(3, 2));

// Convert 24-hour time to 12-hour format
h = h % 12;

// Hour hand moves 0.5 degrees per minute 
// (30 degrees per hour)
double hrAngle = 0.5 * (h * 60 + m);

// Minute hand moves 6 degrees per minute
double minAngle = 6 * m;

// Find the absolute difference between the two angles
double angle = fabs(hrAngle - minAngle);

// Return the smaller angle of the two possible angles
return getMin(360.0 - angle, angle);

}

int main() { string s = "06:00" ;

cout << fixed << setprecision(3);
cout << getAngle(s) << endl;

return 0;

}

Java

import java.text.DecimalFormat;

public class GfG {

// Utility function to return the minimum of two double values
static double getMin(double x, double y) {
    return (x < y) ? x : y;
}

// Function to calculate the minimum angle between
// hour and minute hands
static double getAngle(String s) {
    // Extract hours and minutes from "HH:MM"
    int h = Integer.parseInt(s.substring(0, 2));
    int m = Integer.parseInt(s.substring(3, 5));

    // Convert 24-hour time to 12-hour format
    h = h % 12;

    // Hour hand moves 0.5 degrees per minute 
    // (30 degrees per hour)
    double hrAngle = 0.5 * (h * 60 + m);

    // Minute hand moves 6 degrees per minute
    double minAngle = 6 * m;

    // Find the absolute difference between the two angles
    double angle = Math.abs(hrAngle - minAngle);

    // Return the smaller angle of the two possible angles
    return getMin(360 - angle, angle);
}

public static void main(String[] args) {
    String s = "06:00";
    DecimalFormat df = new DecimalFormat("0.000");
    System.out.println(df.format(getAngle(s)));
}

}

Python

Utility function to return the minimum of two values

def getMin(x, y): return x if x < y else y

Function to calculate the minimum angle between

hour and minute hands

def getAngle(s): # Extract hours and minutes from "HH:MM" h = int(s[:2]) m = int(s[3:])

# Convert 24-hour time to 12-hour format
h = h % 12

# Hour hand moves 0.5 degrees per minute
# (30 degrees per hour)
hrAngle = 0.5 * (h * 60 + m)

# Minute hand moves 6 degrees per minute
minAngle = 6 * m

# Find the absolute difference between the two angles
angle = abs(hrAngle - minAngle)

# Return the smaller angle of the two possible angles
return getMin(360 - angle, angle)

if name == "main": s = "06:00" print(f"{getAngle(s):.3f}")

C#

using System;

class GfG { // Utility function to return the minimum of two double values static double getMin(double x, double y) { return (x < y) ? x : y; }

// Function to calculate the minimum angle between 
// hour and minute hands
static double getAngle(string s)
{
    // Extract hours and minutes from "HH:MM"
    int h = int.Parse(s.Substring(0, 2));
    int m = int.Parse(s.Substring(3, 2));

    // Convert 24-hour time to 12-hour format
    h = h % 12;

    // Hour hand moves 0.5 degrees per minute
    // (30 degrees per hour)
    double hrAngle = 0.5 * (h * 60 + m);

    // Minute hand moves 6 degrees per minute
    double minAngle = 6 * m;

    // Find the absolute difference between the two angles
    double angle = Math.Abs(hrAngle - minAngle);

    // Return the smaller angle of the two possible angles
    return getMin(360 - angle, angle);
}

static void Main()
{
    string s = "06:00";
    Console.WriteLine($"{getAngle(s):F3}");
}

}

JavaScript

// Utility function to return the minimum of two values function getMin(x, y) { return x < y ? x : y; }

// Function to calculate the minimum angle between // hour and minute hands function getAngle(s) { // Extract hours and minutes from "HH:MM" let h = parseInt(s.substring(0, 2)); let m = parseInt(s.substring(3, 5));

// Convert 24-hour time to 12-hour format
h = h % 12;

// Hour hand moves 0.5 degrees per minute
// (30 degrees per hour)
let hrAngle = 0.5 * (h * 60 + m);

// Minute hand moves 6 degrees per minute
let minAngle = 6 * m;

// Find the absolute difference between the two angles
let angle = Math.abs(hrAngle - minAngle);

// Return the smaller angle of the two possible angles
return getMin(360 - angle, angle);

}

// Driver code let s = "06:00"; console.log(getAngle(s).toFixed(3));

`