# Changing One Clock Time to Other Time in Minimum Number of Operations

Given two clock times (in HH:MM:SS format), change one time to other time in minimum number of operations. Here one operation is meant by shifting the hour, minute or second clock hand by 1 unit in either directions. The program follows all the basic rules of a clock for eg.: changing second’s hand from 59 to 0 will make 1 unit change for minute hand also. But this will be considered as only one operation.

Examples:

Input : original_time = "10:10:10", new_time = "05:02:58" Output : 24 Operations = 5 + 7 + 12 = 24 Input : original_time = "13:12:21", new_time = "11:10:18" Output : 7 Operations = 2 + 2 + 3 = 7

The time in clock starts at 00:00:00 and ends at 23:59:59 in 24-hr clock. First of all we convert the given times in seconds. Then finding the difference in seconds. The difference is calculated in two ways. One directly subtracting the smaller time from greater time. The other way is to move the greater time to 23:59:59 and than go to smaller time.

Then finding the operations needed for two differences.

Explanation of the example 1:

original_time = "10:10:10", new_time = "05:02:58" . original time in seconds = 10*3600 + 10*60 + 10 = 36610 new time in seconds = 5*3600 + 2*60 + 58 = 18178 difference1 = 36610 - 18178 = 18432 since there are total 24 hr i.e.. 86400 seconds in a day. difference2 = 86400 - 36610 + 18178 = 67968 Now, to calculate operations first move hour hand, than minute hand and than second hand. operations1 = 5 + 7 + 12 = 24 operations2 = 19 + 7 + 12 = 38 So, minimum of these two is answer. Hence 24 operations.

One main point to note that if seconds left after hour hand movement is greater than 1830 or after minute hand movement is greater than 30 than move that hand one more time.

Example : To calculate operations for 118 seconds. So for 118 seconds, hour hand will not be moved. Moving minute hand. 118/60 = 1 (integer) (minute hand move) 118%60 = 58 (second hand move) So, operations will be 1 + 58 = 59 But according to above statement, 1+1=2 (minute hand move) 60 - 58 = 2 (second hand move) So, operations will be 2 + 2 = 4 Hence 4 will be taken as answer.

## C++

`// C++ program to find minimum number of ` `// operations needed to change one clock ` `// time to other.Here clock is consider ` `// as 24 Hour clock. The time given is ` `// in HH:MM:SS format. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// hh is HH, mm is MM and ss is SS ` `// in HH:MM:SS format respectively. ` `void` `readTime(string ` `time` `, ` `int` `&hh, ` `int` `&mm, ` `int` `ss) ` `{ ` ` ` `stringstream s; ` ` ` ` ` `// c is used to read ":" in given format. ` ` ` `char` `c; ` ` ` ` ` `s.clear(); ` ` ` `s.str(` `time` `); ` ` ` `s >> hh >> c >> mm >> c >> ss; ` `} ` ` ` `// Returns minimum number of operations required ` `// to convert original_time to new_time. ` `int` `findMinOperations(string original_time, ` ` ` `string new_time) ` `{ ` ` ` `int` `hh, mm, ss; ` ` ` ` ` `// Here ots is the original time in ` ` ` `// seconds. ` ` ` `readTime(original_time, hh, mm, ss); ` ` ` `int` `ots = 3600*hh + 60*mm + ss; ` ` ` ` ` ` ` `// Here nts is the new time in ` ` ` `// seconds. ` ` ` `readTime(new_time, hh, mm, ss); ` ` ` `int` `nts = 3600*hh + 60*mm + ss; ` ` ` ` ` `// Here gre and sma is to find which ` ` ` `// time is ahead and which is back ` ` ` `// respectively. ` ` ` `int` `gre = max(ots, nts); ` ` ` `int` `sma = min(ots, nts); ` ` ` ` ` `// diff is array containing two integers. ` ` ` `// One is second's difference between gre and sma. ` ` ` `// The other one is the second's difference when ` ` ` `// the greater will goes up to 24 hr and than come ` ` ` `// back to sma. ` ` ` `int` `diff[2] = {gre - sma, 86400 - (gre - sma)}; ` ` ` ` ` `// ope is array containing two integers, the number ` ` ` `// of operations needed to change the time ` ` ` `// corresponding to two differences. ` ` ` `int` `ope[2]; ` ` ` `for` `(` `int` `i=0; i<2; i++) ` ` ` `{ ` ` ` `// Firstly move the hour hand as much as ` ` ` `// possible. ` ` ` `// This gives the number of operations ` ` ` `// by hour hand. ` ` ` `ope[i] = diff[i]/3600; ` ` ` ` ` `// The seconds left after moving hour ` ` ` `// hand. ` ` ` `diff[i] = diff[i]%3600; ` ` ` ` ` `// If number of seconds left are greater ` ` ` `// than 1830 than move hour hand one more time ` ` ` `if` `(diff[i] > 1830) ` ` ` `{ ` ` ` `ope[i]++; ` ` ` ` ` `// Now seconds left will be: ` ` ` `diff[i]= 3600 - diff[i]; ` ` ` `} ` ` ` ` ` `// Now move the minute hand as much as ` ` ` `// possible. ` ` ` `ope[i] = ope[i]+diff[i]/60; ` ` ` ` ` `// The seconds left after moving minute ` ` ` `// hand. ` ` ` `diff[i] = diff[i]%60; ` ` ` ` ` `// If number of seconds left are greater ` ` ` `// than 30 than move minute hand one more time ` ` ` `if` `(diff[i] > 30) ` ` ` `{ ` ` ` `ope[i]++; ` ` ` ` ` `// Now seconds left will be: ` ` ` `diff[i] = 60-diff[i]; ` ` ` `} ` ` ` `ope[i] = ope[i] + diff[i]; ` ` ` `} ` ` ` ` ` `// The answer will be the minimum of operations ` ` ` `// needed to cover those two differences. ` ` ` `return` `min(ope[0], ope[1]); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string original_time = ` `"10:05:04"` `; ` ` ` `string new_time = ` `"02:34:12"` `; ` ` ` `cout << findMinOperations(original_time, new_time); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum number of ` `// operations needed to change one clock ` `// time to other.Here clock is consider ` `// as 24 Hour clock. The time given is ` `// in HH:MM:SS format. ` ` ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` `import` `java.lang.StringBuilder; ` ` ` ` ` `// Class to Store time in ` `// HH:MM:SS format respectively. ` `class` `ReadTime ` `{ ` ` ` `int` `hh; ` ` ` `int` `mm; ` ` ` `int` `ss; ` `} ` ` ` `/* Name of the class to con time */` `class` `ChangeTime ` `{ ` ` ` `// two object one for each original and ` ` ` `// New time ` ` ` `ReadTime res=` `new` `ReadTime(); ` ` ` `ReadTime res1=` `new` `ReadTime(); ` ` ` ` ` ` ` `// hh is HH, mm is MM and ss is SS ` ` ` `// in HH:MM:SS format respectively. ` ` ` ` ` `public` `void` `readTime(String time, ReadTime res) ` ` ` `{ ` ` ` `String s=time; ` ` ` `String[] values = s.split(` `":"` `); ` ` ` ` ` `res.hh=Integer.parseInt(values[` `0` `].toString()); ` ` ` `res.mm=Integer.parseInt(values[` `1` `].toString()); ` ` ` `res.ss=Integer.parseInt(values[` `2` `].toString()); ` ` ` ` ` `} ` ` ` ` ` ` ` `// Returns minimum number of operations required ` ` ` `// to convert original_time to new_time. ` ` ` `public` `int` `findMinOperations(String original_time, String new_time) ` ` ` `{ ` ` ` ` ` ` ` `// Here ots is the original time in ` ` ` `// seconds. ` ` ` `readTime(original_time, res); ` ` ` `int` `ots = ` `3600` `*res.hh + ` `60` `*res.mm + res.ss; ` ` ` ` ` ` ` `// Here nts is the new time in ` ` ` `// seconds. ` ` ` `readTime(new_time, res1); ` ` ` `int` `nts = ` `3600` `*res1.hh + ` `60` `*res1.mm + res1.ss; ` ` ` ` ` ` ` `// Here gre and sma is to find which ` ` ` `// time is ahead and which is back ` ` ` `// respectively. ` ` ` `int` `gre = Math.max(ots, nts); ` ` ` `int` `sma = Math.min(ots, nts); ` ` ` ` ` `// diff is array containing two integers. ` ` ` `// One is second's difference between gre and sma. ` ` ` `// The other one is the second's difference when ` ` ` `// the greater will goes up to 24 hr and than come ` ` ` `// back to sma. ` ` ` `//int diff[]=new int[5]; ` ` ` `gre=gre - sma; ` ` ` ` ` `int` `diff[] = {gre, ` `86400` `- gre}; ` ` ` ` ` `// ope is array containing two integers, the number ` ` ` `// of operations needed to change the time ` ` ` `// corresponding to two differences. ` ` ` `int` `ope[]=` `new` `int` `[` `2` `]; ` ` ` `for` `(` `int` `i=` `0` `; i<` `2` `; i++) ` ` ` `{ ` ` ` `// Firstly move the hour hand as much as ` ` ` `// possible. ` ` ` `// This gives the number of operations ` ` ` `// by hour hand. ` ` ` `ope[i] = diff[i]/` `3600` `; ` ` ` ` ` `// The seconds left after moving hour ` ` ` `// hand. ` ` ` `diff[i] = diff[i]%` `3600` `; ` ` ` ` ` `// If number of seconds left are greater ` ` ` `// than 1830 than move hour hand one more time ` ` ` `if` `(diff[i] > ` `1830` `) ` ` ` `{ ` ` ` `ope[i]++; ` ` ` ` ` `// Now seconds left will be: ` ` ` `diff[i]= ` `3600` `- diff[i]; ` ` ` `} ` ` ` ` ` `// Now move the minute hand as much as ` ` ` `// possible. ` ` ` `ope[i] = ope[i]+diff[i]/` `60` `; ` ` ` ` ` `// The seconds left after moving minute ` ` ` `// hand. ` ` ` `diff[i] = diff[i]%` `60` `; ` ` ` ` ` `// If number of seconds left are greater ` ` ` `// than 30 than move minute hand one more time ` ` ` `if` `(diff[i] > ` `30` `) ` ` ` `{ ` ` ` `ope[i]++; ` ` ` ` ` `// Now seconds left will be: ` ` ` `diff[i] = ` `60` `-diff[i]; ` ` ` `} ` ` ` `ope[i] = ope[i] + diff[i]; ` ` ` `} ` ` ` ` ` `// The answer will be the minimum of operations ` ` ` `// needed to cover those two differences. ` ` ` `return` `Math.min(ope[` `0` `], ope[` `1` `]); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `String original_time = ` `"10:05:04"` `; ` ` ` `String new_time = ` `"02:34:12"` `; ` ` ` ` ` `ChangeTime obj=` `new` `ChangeTime(); ` ` ` ` ` `System.out.println(obj.findMinOperations(original_time, new_time)); ` ` ` `} ` `} ` ` ` `/* This Code is contributed by Mr. Somesh Awasthi */` |

*chevron_right*

*filter_none*

Output:

45

This article is contributed by **Jatin Goyal**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Convert time from 24 hour clock to 12 hour clock format
- C program to print digital clock with current time
- Time difference between expected time and given time
- Program to find the time after K minutes from given time
- Minimum time required to complete a work by N persons together
- Minimum time required to fill a cistern using N pipes
- Add given n time durations
- Convert given time into words
- Java | Current date and time
- Print system time in C++ (3 different ways)
- Calculate speed, distance and time
- C++ Program to print current Day, Date and Time
- Program to find the time remaining for the day to complete
- Convert timestamp to readable date/time in PHP
- Time Functions in Python | Set-2 (Date Manipulations)