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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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) ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

Lag = 2 minutes

**Time Complexity**: O(1)

## Recommended Posts:

- Find a time for which angle between hour and minute hands is given theta
- Program to find the time after K minutes from given time
- Sum of minimum and the maximum difference between two given Strings
- Difference between two given times
- Changing One Clock Time to Other Time in Minimum Number of Operations
- Difference between Abstract Class and Interface in Java
- Difference between sum of the squares of first n natural numbers and square of sum
- Absolute difference between the first X and last X Digits of N
- Difference between Algorithm, Pseudocode and Program
- Difference between RISC and CISC processor | Set 2
- Difference between fundamental data types and derived data types
- Difference between continue and break statements in C++
- What is the difference between GUI and CUI?
- Difference between print() and println() in Java
- Difference between while and do-while loop in C, C++, Java
- Difference between High Level and Low level languages
- Difference between for and do-while loop in C, C++, Java
- Difference between for and while loop in C, C++, Java
- Difference between Keyword and Identifier
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.