# Distance traveled by Hour and Minute hand in given Time interval

• Last Updated : 17 Jul, 2022

Given two numbers H and M which denotes the length of the hour and minute hand and two time intervals(say T1 & T2) in form of HH:MM, the task is to find the distance traveled by hour hand and minute hand between time T1 and T2.

Examples:

Input: H = 5, M = 7, T1 = “1:30”, T2 = “10:50”
Output:
Distance travelled by minute hand: 410.50144006906635
Distance travelled by hour hand: 24.434609527920614

Input: H = 4, M = 5, T1 = “1:30”, T2 = “10:50”
Output:
Distance travelled by minute hand: 293.21531433504737
Distance travelled by hour hand: 19.54768762233649

Approach:

Total Minutes = hours * 60 + minutes
• Since minute hand covers one rotation in 60 minutes, therefore, number of rotations covered by minute hand(say rm) is given by • Since hour hand covers one rotation in 60*12 = 720 minutes, therefore, number of rotations covered by hour hand(say rh) is given by • The total distance traverse by the hour hand is given by • The total distance traverse by the minute hand is given by Below is the implementation of the above approach:

## C++

 // C++ implementation of above approach#includeusing namespace std; // Function to remove ':' and convert// it into an integerint removeColon(string s){    if (s.length() == 4)        s = s.substr(0,1)+s.substr(2);         if (s.length() == 5)        s = s.substr(0,2) + s.substr(3);         return stoi(s);} // function which find the difference// between time intervalstring diff(string s1,string s2){         // Change string as 2:21 to 221    int time1 = removeColon(s1);    int time2 = removeColon(s2);     // Difference between hours    int hourDiff =    time2 / 100 - time1 / 100 - 1;     // Difference between minutes    int minDiff = time2 % 100 + (60 - time1 % 100);     if (minDiff >= 60){        hourDiff += 1;        minDiff = minDiff - 60;    }     // Convert answer again in string    // with ':'    string res = to_string(hourDiff) + ':' + to_string(minDiff);    return res;} // Function for finding the distance travelled// by minute hand and hour handvector<float> disTravel(string T1,string T2,int M,int H){         // Find the duration between these time    string dur = diff(T1, T2);         // Remove colon from the dur and    // convert in int    int s = removeColon(dur);     // Convert the time in to minute    // hour * 60 + min    int totalMinute = (s/100)*60 + s % 100;     // Count min hand rotation    float rm = float(totalMinute) / 60;     // Count hour hand rotation    float rh = float(totalMinute) / 720;     // Compute distance traveled by min hand    float minuteDistance = rm * 2*2*acos(0.0) * M;     // Compute distance traveled by hour hand    float hourDistance = rh * 2* 2*acos(0.0) * H;         return {minuteDistance, hourDistance};} // Driver Codeint main(){// Given Time Intervalsstring T1 ="1:30";string T2 ="10:50"; // Given hour and minute hand lengthint H = 5;int M = 7; // Function Callvector<float>distance = disTravel(T1, T2, M, H);float minuteDistance = distance;float hourDistance = distance;  // Print the distance traveled by minute// and hour handcout<<"Distance traveled by minute hand: "<

## Java

 /*package whatever //do not write package name here */import java.io.*;class GFG{   // Function to remove ':' and convert// it into an integerstatic int removeColon(String s){    if (s.length() == 4)        s = s.substring(0,1)+s.substring(2);         if (s.length() == 5)        s = s.substring(0,2) + s.substring(3);         return Integer.valueOf(s);} // function which find the difference// between time intervalstatic String diff(String s1,String s2){         // Change string as 2:21 to 221    int time1 = removeColon(s1);    int time2 = removeColon(s2);     // Difference between hours    int hourDiff = time2 / 100 - time1 / 100 - 1;     // Difference between minutes    int minDiff = time2 % 100 + (60 - time1 % 100);     if (minDiff >= 60){        hourDiff += 1;        minDiff = minDiff - 60;    }     // Convert answer again in string    // with ':'    String res = String.valueOf(hourDiff) + ':' + String.valueOf(minDiff);    return res;} // Function for finding the distance travelled// by minute hand and hour handstatic double[] disTravel(String T1,String T2,int M,int H){         // Find the duration between these time    String dur = diff(T1, T2);         // Remove colon from the dur and    // convert in int    int s = removeColon(dur);     // Convert the time in to minute    // hour * 60 + min    int totalMinute = (s/100)*60 + s % 100;     // Count min hand rotation    float rm = (float)totalMinute / (float)60;     // Count hour hand rotation    float rh = (float)totalMinute / (float)720;     // Compute distance traveled by min hand    double minuteDistance = rm * 2*Math.PI * M;     // Compute distance traveled by hour hand    double hourDistance = rh * 2* Math.PI * H;     double[] res = {minuteDistance, hourDistance};         return res;} // Driver codepublic static void main(String args[]){    // Given Time Intervals    String T1 ="1:30";    String T2 ="10:50";     // Given hour and minute hand length    int H = 5;    int M = 7;     // Function Call    double[] distance = disTravel(T1, T2, M, H);    double minuteDistance = distance;    double hourDistance = distance;     // Print the distance traveled by minute    // and hour hand    System.out.printf("Distance traveled by minute hand: %f%n",minuteDistance);    System.out.printf("Distance traveled by hour hand: %f%n",hourDistance);}} // This code is contributed by shinjanpatra.

## Python

 # Python program for the above approach import math # Function to remove ':' and convert# it into an integerdef removeColon(s):    if (len(s) == 4):        s = s[:1]+s[2:]         if (len(s) == 5):        s = s[:2]+s[3:]         return int(s) # function which find the difference# between time intervaldef diff(s1, s2):         # Change string as 2:21 to 221    time1 = removeColon(s1)    time2 = removeColon(s2)     # Difference between hours    hourDiff = time2 // 100 - time1 // 100 - 1;     # Difference between minutes    minDiff = time2 % 100 + (60 - time1 % 100)     if (minDiff >= 60):        hourDiff+= 1        minDiff = minDiff - 60     # Convert answer again in string    # with ':'    res = str(hourDiff) + ':' + str(minDiff)    return res # Function for finding the distance travelled# by minute hand and hour handdef disTravel(T1, T2, M, H):         # Find the duration between these time    dur = diff(T1, T2)         # Remove colon from the dur and    # convert in int    s = removeColon(dur)     # Convert the time in to minute    # hour * 60 + min    totalMinute =(s//100)*60 + s % 100     # Count min hand rotation    rm = totalMinute / 60;     # Count hour hand rotation    rh = totalMinute / 720;     # Compute distance traveled by min hand    minuteDistance = rm * 2* math.pi * M;     # Compute distance traveled by hour hand    hourDistance = rh * 2* math.pi * H;         return minuteDistance, hourDistance  # Driver Code # Given Time IntervalsT1 ="1:30"T2 ="10:50" # Given hour and minute hand lengthH = 5M = 7 # Function CallminuteDistance, hourDistance = disTravel(T1, T2, M, H) # Print the distance traveled by minute# and hour handprint("Distance traveled by minute hand: "      ,minuteDistance)print("Distance traveled by hour hand: "      ,hourDistance )

## C#

 // C# implementation of above approach using System;class GFG {     // Function to remove ':' and convert    // it into an integer    static int removeColon(string s)    {        if (s.Length == 4)            s = s.Substring(0, 1) + s.Substring(2);         if (s.Length == 5)            s = s.Substring(0, 2) + s.Substring(3);         return Convert.ToInt16(s);    }     // function which find the difference    // between time interval    static string diff(string s1, string s2)    {         // Change string as 2:21 to 221        int time1 = removeColon(s1);        int time2 = removeColon(s2);         // Difference between hours        int hourDiff = time2 / 100 - time1 / 100 - 1;         // Difference between minutes        int minDiff = time2 % 100 + (60 - time1 % 100);         if (minDiff >= 60) {            hourDiff += 1;            minDiff = minDiff - 60;        }         // Convert answer again in string        // with ':'        string res = hourDiff.ToString() + ':'                     + minDiff.ToString();        return res;    }     // Function for finding the distance travelled    // by minute hand and hour hand    static float[] disTravel(string T1, string T2, int M,                             int H)    {         // Find the duration between these time        string dur = diff(T1, T2);         // Remove colon from the dur and        // convert in int        int s = removeColon(dur);         // Convert the time in to minute        // hour * 60 + min        int totalMinute = (s / 100) * 60 + s % 100;         // Count min hand rotation        float rm = (float)totalMinute / 60;         // Count hour hand rotation        float rh = (float)totalMinute / 720;         // Compute distance traveled by min hand        double minuteDistance            = rm * 2 * 2 * Math.Acos(0.0) * M;         // Compute distance traveled by hour hand        double hourDistance            = rh * 2 * 2 * Math.Acos(0.0) * H;         return new float { (float)minuteDistance,                              (float)hourDistance };    }     // Driver Code    static void Main()    {        // Given Time Intervals        string T1 = "1:30";        string T2 = "10:50";         // Given hour and minute hand length        int H = 5;        int M = 7;         // Function Call        float[] distance = disTravel(T1, T2, M, H);        float minuteDistance = distance;        float hourDistance = distance;         // Print the distance traveled by minute        // and hour hand        Console.WriteLine(            "Distance traveled by minute hand: "            + minuteDistance);        Console.WriteLine("Distance traveled by hour hand: "                          + hourDistance);    }} // The code is contributed by Gautam goel (gautamgoel962)

## Javascript

 

Output:

Distance traveled by minute hand:  410.50144006906635
Distance traveled by hour hand:  24.434609527920614`

Time complexity: O(1)

space Complexity: O(1)

My Personal Notes arrow_drop_up