Minimum minutes needed to make the time palindromic
Given string str which stores the time in the 24 hours format as “HH: MM”. The task is to find the minimum minutes that need 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: 0
Explanation: Since, 13:31 is already palindromic therefore, 0 minutes is required
Approach:
The idea is to greedily increment the minute value until the 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 values 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++
#include <bits/stdc++.h>
using namespace std;
int get_palindrome_time(string str)
{
int hh, mm;
hh
= (str[0] - 48) * 10
+ (str[1] - 48);
mm
= (str[3] - 48) * 10
+ (str[4] - 48);
int requiredTime = 0;
while (hh % 10 != mm / 10
|| hh / 10 != mm % 10) {
++mm;
if (mm == 60) {
mm = 0;
++hh;
}
if (hh == 24)
hh = 0;
++requiredTime;
}
return requiredTime;
}
int main()
{
string str = "05:39" ;
cout << get_palindrome_time(str)
<< endl;
}
|
Java
class GFG{
public static int get_palindrome_time(String str)
{
int hh, mm;
hh = (str.charAt( 0 ) - 48 ) * 10 +
(str.charAt( 1 ) - 48 );
mm = (str.charAt( 3 ) - 48 ) * 10 +
(str.charAt( 4 ) - 48 );
int requiredTime = 0 ;
while (hh % 10 != mm / 10 ||
hh / 10 != mm % 10 )
{
++mm;
if (mm == 60 )
{
mm = 0 ;
++hh;
}
if (hh == 24 )
hh = 0 ;
++requiredTime;
}
return requiredTime;
}
public static void main(String[] args)
{
String str = "05:39" ;
System.out.println(get_palindrome_time(str));
}
}
|
Python3
def get_palindrome_time( str ):
hh = (( ord ( str [ 0 ]) - 48 ) * 10 +
( ord ( str [ 1 ]) - 48 ))
mm = (( ord ( str [ 3 ]) - 48 ) * 10 +
( ord ( str [ 4 ]) - 48 ))
requiredTime = 0
while (hh % 10 ! = mm / / 10 or
hh / / 10 ! = mm % 10 ):
mm + = 1
if (mm = = 60 ):
mm = 0
hh + = 1
if (hh = = 24 ):
hh = 0
requiredTime + = 1 ;
return requiredTime
if __name__ = = "__main__" :
str = "05:39" ;
print (get_palindrome_time( str ));
|
C#
using System;
class GFG{
public static int get_palindrome_time( string str)
{
int hh, mm;
hh = (str[0] - 48) * 10 +
(str[1] - 48);
mm = (str[3] - 48) * 10 +
(str[4] - 48);
int requiredTime = 0;
while (hh % 10 != mm / 10 ||
hh / 10 != mm % 10)
{
++mm;
if (mm == 60)
{
mm = 0;
++hh;
}
if (hh == 24)
hh = 0;
++requiredTime;
}
return requiredTime;
}
public static void Main( string [] args)
{
string str = "05:39" ;
Console.Write(get_palindrome_time(str));
}
}
|
Javascript
<script>
function get_palindrome_time(str)
{
let hh, mm;
hh = (str[0].charCodeAt() - 48) * 10 +
(str[1].charCodeAt() - 48);
mm = (str[3].charCodeAt() - 48) * 10 +
(str[4].charCodeAt() - 48);
let requiredTime = 0;
while (hh % 10 != Math.floor(mm / 10) ||
Math.floor(hh / 10) != mm % 10)
{
++mm;
if (mm == 60)
{
mm = 0;
++hh;
}
if (hh == 24)
hh = 0;
++requiredTime;
}
return requiredTime;
}
let str = "05:39" ;
document.write(get_palindrome_time(str.split( '' )));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
11 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...