Open In App

Distance traveled by Hour and Minute hand in given Time interval

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

Below is the implementation of the above approach:

// C++ implementation of above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to remove ':' and convert
// it into an integer
int 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 interval
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 = to_string(hourDiff) + ':' + to_string(minDiff);
    return res;
}
 
// Function for finding the distance travelled
// by minute hand and hour hand
vector<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 Code
int 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
vector<float>distance = disTravel(T1, T2, M, H);
float minuteDistance = distance[0];
float hourDistance = distance[1];
 
 
// Print the distance traveled by minute
// and hour hand
cout<<"Distance traveled by minute hand: "<<minuteDistance<<endl;
cout<<"Distance traveled by hour hand: "<<hourDistance<<endl;
}
 
// This code is contributed by shinjanpatra

                    
/*package whatever //do not write package name here */
import java.io.*;
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 Integer.valueOf(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 = String.valueOf(hourDiff) + ':' + String.valueOf(minDiff);
    return res;
}
 
// Function for finding the distance travelled
// by minute hand and hour hand
static 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 code
public 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[0];
    double hourDistance = distance[1];
 
    // 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 program for the above approach
 
import math
 
# Function to remove ':' and convert
# it into an integer
def 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 interval
def 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 hand
def 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 Intervals
T1 ="1:30"
T2 ="10:50"
 
# Given hour and minute hand length
H = 5
M = 7
 
# Function Call
minuteDistance, hourDistance = disTravel(T1, T2, M, H)
 
# Print the distance traveled by minute
# and hour hand
print("Distance traveled by minute hand: "
      ,minuteDistance)
print("Distance traveled by hour hand: "
      ,hourDistance )

                    
// 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[2] { (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[0];
        float hourDistance = distance[1];
 
        // 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)

                    
<script>
 
// JavaScript implementation of above approach
 
// Function to remove ':' and convert
// it into an integer
function removeColon(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 parseInt(s)
}
 
// function which find the difference
// between time interval
function diff(s1, s2){
     
    // Change string as 2:21 to 221
    let time1 = removeColon(s1)
    let time2 = removeColon(s2)
 
    // Difference between hours
    let hourDiff =     Math.floor(time2 / 100) - Math.floor(time1 / 100) - 1;
 
    // Difference between minutes
    let minDiff = time2 % 100 + (60 - time1 % 100)
 
    if (minDiff >= 60){
        hourDiff += 1
        minDiff = minDiff - 60
    }
 
    // Convert answer again in string
    // with ':'
    let res = hourDiff.toString() + ':' + minDiff.toString()
    return res
}
 
// Function for finding the distance travelled
// by minute hand and hour hand
function disTravel(T1, T2, M, H)
{
     
    // Find the duration between these time
    let dur = diff(T1, T2)
     
    // Remove colon from the dur and
    // convert in int
    let s = removeColon(dur)
 
    // Convert the time in to minute
    // hour * 60 + min
    let totalMinute = Math.floor(s/100)*60 + s % 100
 
    // Count min hand rotation
    let rm = totalMinute / 60;
 
    // Count hour hand rotation
    let rh = totalMinute / 720;
 
    // Compute distance traveled by min hand
    let minuteDistance = rm * 2* Math.PI * M;
 
    // Compute distance traveled by hour hand
    let hourDistance = rh * 2* Math.PI * H;
     
    return [minuteDistance, hourDistance]
}
 
// Driver Code
 
// Given Time Intervals
let T1 ="1:30"
let T2 ="10:50"
 
// Given hour and minute hand length
let H = 5
let M = 7
 
// Function Call
let [minuteDistance, hourDistance] = [...disTravel(T1, T2, M, H)]
 
// Print the distance traveled by minute
// and hour hand
document.write("Distance traveled by minute hand: "
    ,minuteDistance,"</br>")
document.write("Distance traveled by hour hand: "
    ,hourDistance,"</br>")
 
// This code is contributed by shinjanpatra
 
</script>

                    

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

 

Time complexity: O(1)

space Complexity: O(1)


Article Tags :