Minimum minutes needed to make the time palindromic

Given a string str which stores the time in the 24 hours fomat as “HH:MM”. The task is to find the minimum minutes needs to be added to make time palindromic.

Examples:

Input: str = “05:39”
Output: 11
Explanation: It takes 11 minutes for minute value to become 50, 05:50 is a palindromic time

Examples:
Input: str = “13:31”
Output:
Explanation: Since, 13:31 is already palindromic therefore, 0 minutes is required

Approach:
The idea is to greedily increment the minute value until time value becomes palindrome. Run a while loop to increment minute value and simultaneously check if hour value and minute value form a palindrome or not.
While incrementing minute and hour value make sure to check for the base condition when the minute value is 60 and the hour value is 24.

Below is the implementation of the above approach :

C++

 `// C++ program for the above approach  ` `#include   ` `using` `namespace` `std;  ` ` `  `// Function to get the required minutes  ` `int` `get_palindrome_time(string str)  ` `{  ` `    ``int` `hh, mm;  ` ` `  `    ``// Storing hour and minute value  ` `    ``// in integral form  ` `    ``hh  ` `        ``= (str[0] - 48) * 10  ` `        ``+ (str[1] - 48);  ` `    ``mm  ` `        ``= (str[3] - 48) * 10  ` `        ``+ (str[4] - 48);  ` ` `  `    ``int` `requiredTime = 0;  ` ` `  `    ``// Keep iterating till first digit  ` `    ``// hour becomes equal to second  ` `    ``// digit of minute and second digit  ` `    ``// of hour becomes equal to first  ` `    ``// digit of minute  ` `    ``while` `(hh % 10 != mm / 10  ` `        ``|| hh / 10 != mm % 10) {  ` ` `  `        ``++mm;  ` ` `  `        ``// If mins is 60, increase hour, and  ` `        ``// reinitilialized to 0  ` `        ``if` `(mm == 60) {  ` `            ``mm = 0;  ` `            ``++hh;  ` `        ``}  ` ` `  `        ``// If hours is 60, reinitialized to 0  ` `        ``if` `(hh == 24)  ` `            ``hh = 0;  ` `        ``++requiredTime;  ` `    ``}  ` ` `  `    ``// Return the required time  ` `    ``return` `requiredTime;  ` `}  ` ` `  `// Driver Code  ` `int` `main()  ` `{  ` `    ``// Given Time as a string  ` `    ``string str = ``"05:39"``;  ` ` `  `    ``// Function Call  ` `    ``cout << get_palindrome_time(str)  ` `        ``<< endl;  ` `}  `

Java

 `// Java program for the above approach  ` `class` `GFG{  ` `     `  `// Function to get the required minutes  ` `public` `static` `int` `get_palindrome_time(String str)  ` `{  ` `    ``int` `hh, mm;  ` ` `  `    ``// Storing hour and minute value  ` `    ``// in integral form  ` `    ``hh = (str.charAt(``0``) - ``48``) * ``10` `+  ` `        ``(str.charAt(``1``) - ``48``);  ` `    ``mm = (str.charAt(``3``) - ``48``) * ``10` `+  ` `        ``(str.charAt(``4``) - ``48``);  ` ` `  `    ``int` `requiredTime = ``0``;  ` ` `  `    ``// Keep iterating till first digit  ` `    ``// hour becomes equal to second  ` `    ``// digit of minute and second digit  ` `    ``// of hour becomes equal to first  ` `    ``// digit of minute  ` `    ``while` `(hh % ``10` `!= mm / ``10` `||  ` `        ``hh / ``10` `!= mm % ``10``)  ` `    ``{  ` `        ``++mm;  ` ` `  `        ``// If mins is 60, increase hour, and  ` `        ``// reinitilialized to 0  ` `        ``if` `(mm == ``60``)  ` `        ``{  ` `            ``mm = ``0``;  ` `            ``++hh;  ` `        ``}  ` ` `  `        ``// If hours is 60, reinitialized to 0  ` `        ``if` `(hh == ``24``)  ` `            ``hh = ``0``;  ` `        ``++requiredTime;  ` `    ``}  ` ` `  `    ``// Return the required time  ` `    ``return` `requiredTime;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `     `  `    ``// Given Time as a string  ` `    ``String str = ``"05:39"``;  ` ` `  `    ``// Function Call  ` `    ``System.out.println(get_palindrome_time(str));  ` `}  ` `}  ` ` `  `// This code is contributed by divyeshrabadiya07  `

Python3

 `# Python3 program for the above approach  ` ` `  `# Function to get the required minutes   ` `def` `get_palindrome_time(``str``): ` `     `  `    ``# Storing hour and minute value  ` `    ``# in integral form ` `    ``hh ``=` `((``ord``(``str``[``0``]) ``-` `48``) ``*` `10` `+`  `          ``(``ord``(``str``[``1``]) ``-` `48``)) ` `    ``mm ``=` `((``ord``(``str``[``3``]) ``-` `48``) ``*` `10` `+` `          ``(``ord``(``str``[``4``]) ``-` `48``)) ` `     `  `    ``requiredTime ``=` `0` ` `  `    ``# Keep iterating till first digit  ` `    ``# hour becomes equal to second  ` `    ``# digit of minute and second digit  ` `    ``# of hour becomes equal to first  ` `    ``# digit of minute  ` `    ``while` `(hh ``%` `10` `!``=` `mm ``/``/` `10` `or`  `          ``hh ``/``/` `10` `!``=` `mm ``%` `10``): ` `        ``mm ``+``=` `1` ` `  `        ``# If mins is 60, increase hour, and  ` `        ``# reinitilialized to 0  ` `        ``if` `(mm ``=``=` `60``): ` `            ``mm ``=` `0` `            ``hh ``+``=` `1` ` `  `        ``# If hours is 60, reinitialized to 0  ` `        ``if` `(hh ``=``=` `24``): ` `            ``hh ``=` `0` `             `  `        ``requiredTime ``+``=` `1``;  ` `         `  `    ``# Return the required time  ` `    ``return` `requiredTime ` `     `  `if` `__name__``=``=``"__main__"``: ` `     `  `    ``# Given Time as a string  ` `    ``str` `=` `"05:39"``;  ` ` `  `    ``# Function call  ` `    ``print``(get_palindrome_time(``str``)); ` ` `  `# This code is contributed by rutvik_56 `

C#

 `// C# program for the above approach  ` `using` `System;  ` ` `  `class` `GFG{  ` `     `  `// Function to get the required minutes  ` `public` `static` `int` `get_palindrome_time(``string` `str)  ` `{  ` `    ``int` `hh, mm;  ` ` `  `    ``// Storing hour and minute value  ` `    ``// in integral form  ` `    ``hh = (str[0] - 48) * 10 +  ` `        ``(str[1] - 48);  ` `    ``mm = (str[3] - 48) * 10 +  ` `        ``(str[4] - 48);  ` ` `  `    ``int` `requiredTime = 0;  ` ` `  `    ``// Keep iterating till first digit  ` `    ``// hour becomes equal to second  ` `    ``// digit of minute and second digit  ` `    ``// of hour becomes equal to first  ` `    ``// digit of minute  ` `    ``while` `(hh % 10 != mm / 10 ||  ` `        ``hh / 10 != mm % 10)  ` `    ``{  ` `        ``++mm;  ` ` `  `        ``// If mins is 60, increase hour,  ` `        ``// and reinitilialized to 0  ` `        ``if` `(mm == 60)  ` `        ``{  ` `            ``mm = 0;  ` `            ``++hh;  ` `        ``}  ` ` `  `        ``// If hours is 60, reinitialized to 0  ` `        ``if` `(hh == 24)  ` `            ``hh = 0;  ` `        ``++requiredTime;  ` `    ``}  ` ` `  `    ``// Return the required time  ` `    ``return` `requiredTime;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main(``string``[] args)  ` `{  ` `     `  `    ``// Given Time as a string  ` `    ``string` `str = ``"05:39"``;  ` ` `  `    ``// Function Call  ` `    ``Console.Write(get_palindrome_time(str));  ` `}  ` `}  ` ` `  `// This code is contributed by rutvik_56  `

Output:

```11
```

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.