# Distance between end points of Hour and minute hand at given time

• Last Updated : 22 Sep, 2022

Given four integers H, M, L1, and L2, which denotes the time as an hour and minutes in a Clock of 12-Hour format and L1 and L2 denotes the length of the hour hand and minute hand respectively. The task is to find the distance between the endpoints of the hour and minutes hand.
Examples:

Input: H = 3, M = 30, L1 = 3, L2 = 4
Output: 4.33499
Explanation:
At 3:30, distance between end point of hour hand and minute hand is 4.33499 Input: H = 10, M = 30, L1 = 3, L2 = 4
Output: 6.47898
Explanation:
At 10:30, distance between end point of hour hand and minute hand is 6.47898

Approach: The idea is to find the angle between the hour hand and minute hand at the given time and then the distance between the endpoints of the hour hand and minute hand can be computed with the help of the cosine formulae –

Distance Between End Points of the Hour and minute hand = Below is the implementation of the above approach:

## C++

 // C++ implementation to find the// distance between the end points// of the hour and minute hand #include using namespace std; // Function to find the angle between// Hour hand and minute handint calcAngle(double h, double m){    // Validate the input    if (h < 0 || m < 0        || h > 12 || m > 60)        printf("Wrong input");     if (h == 12)        h = 0;    if (m == 60)        m = 0;     // Calculate the angles moved    // by hour and minute hands    // with reference to 12:00    int hour_angle = 0.5 * (h * 60 + m);    int minute_angle = 6 * m;     // Find the difference    // between two angles    int angle = abs(hour_angle - minute_angle);     // Return the smaller angle    // of two possible angles    angle = min(360 - angle, angle);     return angle;} // Function to calculate// cos value of angle cfloat cal_cos(float n){    float accuracy = 0.0001, x1,          denominator, cosx, cosval;     // Converting degrees to radian    n = n * (3.142 / 180.0);     x1 = 1;     // Maps the sum    // along the series    cosx = x1;     // Holds the actual    // value of sin(n)    cosval = cos(n);    int i = 1;    do {        denominator = 2 * i * (2 * i - 1);        x1 = -x1 * n * n / denominator;        cosx = cosx + x1;        i = i + 1;    } while (accuracy <= fabs(cosval - cosx));     return cosx;} // Function to distance between the// endpoints of the hour and minute handfloat distanceEndpoints(    int a, int b, float c){    float angle = cal_cos(c);    return sqrt((a * a)                + (b * b)                - 2 * a * b * angle);} // Driver Codeint main(){    // Time    int hour = 3;    int min = 30;     // Length of    // hour hand    int hourHand = 3;     // Length of    // minute hand    int minHand = 4;     // calling Function for    // finding angle    // between hour hand    // and minute hand    double angle = calcAngle(hour, min);     // Function for finding    // distance between    // end points of minute    // hand and hour hand    float distance = distanceEndpoints(        minHand, hourHand, angle);    cout << distance;    return 0;}

## Java

 // Java implementation to find the// distance between the end points// of the hour and minute handclass GFG{ // Function to find the angle between// Hour hand and minute handstatic int calcAngle(double h, double m){         // Validate the input    if (h < 0 || m < 0 ||        h > 12 || m > 60)        System.out.printf("Wrong input");     if (h == 12)        h = 0;    if (m == 60)        m = 0;     // Calculate the angles moved    // by hour and minute hands    // with reference to 12:00    int hour_angle = (int)(0.5 * (h * 60 + m));    int minute_angle = (int)(6 * m);     // Find the difference    // between two angles    int angle = Math.abs(hour_angle -                         minute_angle);     // Return the smaller angle    // of two possible angles    angle = Math.min(360 - angle, angle);     return angle;} // Function to calculate// cos value of angle cstatic float cal_cos(float n){    float accuracy = (float) 0.0001, x1,                            denominator,                           cosx, cosval;     // Converting degrees to radian    n = (float)(n * (3.142 / 180.0));     x1 = 1;     // Maps the sum along    // the series    cosx = x1;     // Holds the actual    // value of sin(n)    cosval = (float)Math.cos(n);    int i = 1;         do    {        denominator = 2 * i * (2 * i - 1);        x1 = -x1 * n * n / denominator;        cosx = cosx + x1;        i = i + 1;    } while (accuracy <= Math.abs(cosval - cosx));     return cosx;} // Function to distance between the// endpoints of the hour and minute handstatic float distanceEndpoints(int a, int b,                                    float c){    float angle = cal_cos(c);    return (float) Math.sqrt((a * a) +                             (b * b) -                              2 * a * b * angle);} // Driver codepublic static void main(String[] args){         // Time    int hour = 3;    int min = 30;     // Length of    // hour hand    int hourHand = 3;     // Length of    // minute hand    int minHand = 4;     // Calling Function    // for finding angle    // between hour hand    // and minute hand    double angle = calcAngle(hour, min);     // Function for finding    // distance between    // end points of minute    // hand and hour hand    float distance = distanceEndpoints(minHand,                                       hourHand,                                       (long)angle);    System.out.printf("%.5f", distance);}} // This code is contributed by 29AjayKumar

## Python3

 # Python3 implementation to find the# distance between the end points# of the hour and minute handimport math # Function to find the angle# between Hour hand and minute# handdef calcAngle(h, m):     # Validate the input    if (h < 0 or m < 0 or        h > 12 or m > 60):        print("Wrong input")     if (h == 12):        h = 0    if (m == 60):        m = 0     # Calculate the angles moved    # by hour and minute hands    # with reference to 12:00    hour_angle = 0.5 * (h * 60 + m)    minute_angle = 6 * m     # Find the difference    # between two angles    angle = abs(hour_angle -                minute_angle)     # Return the smaller angle    # of two possible angles    angle = min(360 -                angle, angle)         return angle # Function to calculate# cos value of angle cdef cal_cos(n):     accuracy = 0.0001     # Converting degrees to    # radian    n = n * (3.142 / 180.0)     x1 = 1     # Maps the sum    # along the series    cosx = x1     # Holds the actual    # value of sin(n)    cosval = math.cos(n)    i = 1         while True:        denominator = 2 * i * (2 * i - 1)        x1 = -x1 * n * n / denominator        cosx = cosx + x1        i = i + 1                 if accuracy > math.fabs(cosval -                                cosx):            break         return cosx # Function to distance between# the endpoints of the hour# and minute handdef distanceEndpoints(a, b, c):         angle = cal_cos(c)    return math.sqrt((a * a) +                     (b * b) -                     2 * a * b * angle) # Driver code # Timehour = 3Min = 30 # Length of# hour handhourHand = 3 # Length of# minute handminHand = 4 # calling Function for# finding angle# between hour hand# and minute handangle = calcAngle(hour,                  Min) # Function for finding# distance between# end points of minute# hand and hour handdistance = distanceEndpoints(minHand,                             hourHand,                             angle)print ('%.5f' % distance) # This code is contributed by divyeshrabadiya07

## C#

 // C# implementation to find the// distance between the end points// of the hour and minute handusing System;class GFG{ // Function to find the angle between// Hour hand and minute handstatic int calcAngle(double h, double m){         // Validate the input    if (h < 0 || m < 0 ||        h > 12 || m > 60)        Console.Write("Wrong input");     if (h == 12)        h = 0;    if (m == 60)        m = 0;     // Calculate the angles moved    // by hour and minute hands    // with reference to 12:00    int hour_angle = (int)(0.5 * (h * 60 + m));    int minute_angle = (int)(6 * m);     // Find the difference    // between two angles    int angle = Math.Abs(hour_angle -                       minute_angle);     // Return the smaller angle    // of two possible angles    angle = Math.Min(360 - angle, angle);     return angle;} // Function to calculate// cos value of angle cstatic float cal_cos(float n){    float accuracy = (float) 0.0001, x1,                            denominator,                           cosx, cosval;     // Converting degrees to radian    n = (float)(n * (3.142 / 180.0));     x1 = 1;     // Maps the sum along    // the series    cosx = x1;     // Holds the actual    // value of sin(n)    cosval = (float)Math.Cos(n);    int i = 1;         do    {        denominator = 2 * i * (2 * i - 1);        x1 = -x1 * n * n / denominator;        cosx = cosx + x1;        i = i + 1;    } while (accuracy <= Math.Abs(cosval - cosx));     return cosx;} // Function to distance between the// endpoints of the hour and minute handstatic float distanceEndpoints(int a, int b,                                    float c){    float angle = cal_cos(c);    return (float) Math.Sqrt((a * a) +                             (b * b) -                          2 * a * b * angle);} // Driver codepublic static void Main(){         // Time    int hour = 3;    int min = 30;     // Length of    // hour hand    int hourHand = 3;     // Length of    // minute hand    int minHand = 4;     // Calling Function    // for finding angle    // between hour hand    // and minute hand    double angle = calcAngle(hour, min);     // Function for finding    // distance between    // end points of minute    // hand and hour hand    float distance = distanceEndpoints(minHand,                                      hourHand,                                  (long)angle);    Console.Write(distance);}} // This code is contributed by Code_Mech

## Javascript

 

Output:

4.33499

Time complexity: O(logn) because sqrt function has been
Auxiliary space: O(1)

My Personal Notes arrow_drop_up