Find the time which is palindromic and comes after the given time | C ++ implementation of the approach ; Function to return the required time ; To store the resultant time ; Hours are stored in h as integer ; Minutes are stored in m as integer ; Reverse of h ; Reverse of h as a string ; If MM < reverse of ( HH ) ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Increment hours ; Reverse of the hour after incrementing 1 ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long ll ; string getTime ( string s , int n ) { string res ; int h = stoi ( s . substr ( 0 , 2 ) ) ; int m = stoi ( s . substr ( 3 , 2 ) ) ; int rev_h = ( h % 10 ) * 10 + ( ( h % 100 ) - ( h % 10 ) ) / 10 ; string rev_hs = to_string ( rev_h ) ; if ( h == 23 && m >= 32 ) { res = " - 1" ; } else if ( m < rev_h ) { string temp ; if ( h < 10 ) temp = "0" ; temp = temp + to_string ( h ) ; if ( rev_h < 10 ) res = res + temp + " : 0" + rev_hs ; else res = res + temp + " : " + rev_hs ; } else { h ++ ; rev_h = ( h % 10 ) * 10 + ( ( h % 100 ) - ( h % 10 ) ) / 10 ; rev_hs = to_string ( rev_h ) ; string temp ; if ( h < 10 ) temp = "0" ; temp = temp + to_string ( h ) ; if ( rev_h < 10 ) res = res + temp + " : 0" + rev_hs ; else res = res + temp + " : " + rev_hs ; } return res ; } int main ( ) { string s = "21:12" ; int n = s . length ( ) ; cout << getTime ( s , n ) ; return 0 ; }