Time difference between expected time and given time Last Updated : 13 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given the initial clock time h1:m1 and the present clock time h2:m2, denoting hour and minutes in 24-hours clock format. The present clock time h2:m2 may or may not be correct. Also given a variable K which denotes the number of hours passed. The task is to calculate the delay in seconds i.e. time difference between expected time and given time. Examples : Input: h1 = 10, m1 = 12, h2 = 10, m2 = 17, k = 2 Output: 115 minutes The clock initially displays 10:12. After 2 hours it must show 12:12. But at this point, the clock displays 10:17. Hence, the clock must be lagging by 115 minutes. so the answer is 115.Input: h1 = 12, m1 = 00, h2 = 12, m2 = 58, k = 1 Output: 2 minutes The clock initially displays 12:00. After 1 hour it must show 13:00. But at this point, the clock displays 12:58. Hence, the clock must be lagging by 2 minutes. so the answer is 2. Approach: Convert given time in h:m format to number of minutes. It is simply 60*h+m.Calculate both the computed time(adding K hours to the initial time).Find the difference in minutes which will be the answer. Below is the implementation of the above approach. C++ // C++ program to calculate clock delay #include <bits/stdc++.h> // Function definition with logic int lagDuration(int h1, int m1, int h2, int m2, int k) { int lag, t1, t2; // Conversion to minutes t1 = (h1 + k) * 60 + m1; // Conversion to minutes t2 = h2 * 60 + m2; // Calculating difference lag = t1 - t2; return lag; } // Driver Code int main() { int h1 = 12, m1 = 0; int h2 = 12, m2 = 58; int k = 1; int lag = lagDuration(h1, m1, h2, m2, k); printf("Lag = %d minutes", lag); return 0; } Java // Java program to // calculate clock delay class GFG { // Function definition // with logic static int lagDuration(int h1, int m1, int h2, int m2, int k) { int lag, t1, t2; // Conversion to minutes t1 = (h1 + k) * 60 + m1; // Conversion to minutes t2 = h2 * 60 + m2; // Calculating difference lag = t1 - t2; return lag; } // Driver Code public static void main(String args[]) { int h1 = 12, m1 = 0; int h2 = 12, m2 = 58; int k = 1; int lag = lagDuration(h1, m1, h2, m2, k); System.out.println("Lag = " + lag + " minutes"); } } // This code is contributed // by Kirti_Mangal Python3 # Python3 program to calculate clock delay # Function definition with logic def lagDuration(h1, m1, h2, m2, k): lag, t1, t2 = 0, 0, 0 # Conversion to minutes t1 = (h1 + k) * 60 + m1 # Conversion to minutes t2 = h2 * 60 + m2 # Calculating difference lag = t1 - t2 return lag # Driver Code h1, m1 = 12, 0 h2, m2 = 12, 58 k = 1 lag = lagDuration(h1, m1, h2, m2, k) print("Lag =", lag, "minutes") # This code has been contributed # by 29AjayKumar C# // C# program to // calculate clock delay using System; class GFG { // Function definition // with logic static int lagDuration(int h1, int m1, int h2, int m2, int k) { int lag, t1, t2; // Conversion to minutes t1 = (h1 + k) * 60 + m1; // Conversion to minutes t2 = h2 * 60 + m2; // Calculating difference lag = t1 - t2; return lag; } // Driver Code public static void Main() { int h1 = 12, m1 = 0; int h2 = 12, m2 = 58; int k = 1; int lag = lagDuration(h1, m1, h2, m2, k); Console.WriteLine("Lag = " + lag + " minutes"); } } // This code is contributed // by Akanksha Rai(Abby_akku) PHP <?php // PHP program to // calculate clock delay function lagDuration($h1, $m1, $h2, $m2, $k) { // Conversion to minutes $t1 = ($h1 + $k) * 60 + $m1; // Conversion to minutes $t2 = $h2 * 60 + $m2; // Calculating difference $lag = $t1 - $t2; return $lag; } // Driver Code $h1 = 12; $m1 = 0; $h2 = 12; $m2 = 58; $k = 1; $lag = lagDuration($h1, $m1, $h2, $m2, $k); echo "Lag = $lag minutes"; // This code is contributed // by Kirti_Mangal JavaScript <script> // javascript program to // calculate clock delay // Function definition // with logic function lagDuration(h1 , m1 , h2 , m2 , k) { var lag, t1, t2; // Conversion to minutes t1 = (h1 + k) * 60 + m1; // Conversion to minutes t2 = h2 * 60 + m2; // Calculating difference lag = t1 - t2; return lag; } // Driver Code var h1 = 12, m1 = 0; var h2 = 12, m2 = 58; var k = 1; var lag = lagDuration(h1, m1, h2, m2, k); document.write("Lag = " + lag + " minutes"); // This code is contributed by aashish1995 </script> Output: Lag = 2 minutes Time Complexity: O(1)Auxiliary Space: O(1) as it is using constant space for variables Comment More infoAdvertise with us Next Article Total time to pick elements with given time interval T Tarandeep Singh 4 Follow Improve Article Tags : DSA Similar Reads Time Difference between given times in HH:MM:SS format Given 2 times 'st' and 'et' in HH: MM: SS format. The task is to print the time difference between st and et in HH:MM: SS format Examples: Input: st = 13:50:45, et = 14:55:50Output: 01:05:05Explanation: The time gap is 1 hour 5 minutes and 5 seconds. Input: st = 12:00:00, et = 24:00:00Output: 12:00: 6 min read Total time to pick elements with given time interval Given an array arr[] of size N that denotes the type of an element, and another time array time[] of size N where time[i] denotes the time interval between picking two elements of the ith type. The task is to find the time taken to pick all the elements while traversing from left to right of the arr 6 min read Measure execution time of a function in C++ We can find out the time taken by different parts of a program by using the std::chrono library introduced in C++ 11. We have discussed at How to measure time taken by a program in C. The functions described there are supported in C++ too but they are C specific. For clean and robust C++ programs we 3 min read Find the minimum time after which one can exchange notes Given n number of cashiers exchanging the money. At the moment, i^{th} cashier had k_{i} number of people in front of him. The j^{th} person in the line to i^{th} cashier had m_{i,j} notes. Find, how early can one exchange his notes.Time taken by the cashiers: The cashier took 5 seconds to scan a si 8 min read Minimum time to pick all elements with a cooldown period Given an array arr[] of size n, where each element in the array represents a value between 1 and n. Additionally, you are given another array time[], where time[i] (1 ⤠i ⤠n) represents the minimum time required before you can pick the ith element again after you have already picked it, the task is 7 min read Completion time of a given process in round robin We are given n-processes with their completion times in form of an array. We need to find the time instant when a given process p ends if the scheduling process is round robin and time slice is 1-sec. note : Array index start with 0. Examples : Input : arr[] = {3, 2, 4, 2}, p = 1 Output : Completion 8 min read Like