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.
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.
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 email@example.com. 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.
- 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)