Program to print next palindromic Time

By | November 26, 2024

For the given hour and minute let say “hh” and “mm” print the next sooner palindromic time.

Example:

Input: hh = 02
mm = 03
Output: 02:20
Explaination:For the hour 02 and minute 03 the next sooner palindromic time is 02:20

Input hh = 04
mm = 45
Output: 05:50
Explaination:For the hour 04 and minute 45 the next sooner palindromic time is 05:50

Approach:

  1. Start from hour”00″ if hour is 00 print 01:10.
  2. From hour 1 check for minute if minute is less the 10 then print 01:10 else print 02:20.
  3. check for each hour till hour 5 and perform same operation as shown in step2
  4. For hour 6,78 and 9 print 10:01 (there is only one next sooner palindromic time is possible).
  5. For the hour 10 check if minute is “00” then return 10:01else return 11:11
  6. For the hour 11 to 16 perform same operation as shown in the above step and print there possible next sooner palindromic time.
  7. For the hour 16 to 19
    next sooner palindromic time is 20:02
  8. For the hour 20 check if minute is less then 2 then print
    20:02 else print 21:12.
  9. From hour 21 to 23 perform same operation as shown in above step by checking for each minute and print next sooner possible palindromic time.

Below is the c++ implimation of above approach.

// C++ program to print the next sooner palindromic time
#include <iostream>
using namespace std;

// Function to print the next palindromic time for the given input time
void findNextPalindromicTime(int hh, int mm) {
    if (hh == 0) cout << "01:10";
    else if (hh == 1) {
        if (mm < 10) cout << "01:10";
        else cout << "02:20";
    } else if (hh == 2) {
        if (mm < 20) cout << "02:20";
        else cout << "03:30";
    } else if (hh == 3) {
        if (mm < 30) cout << "03:30";
        else cout << "04:40";
    } else if (hh == 4) {
        if (mm < 40) cout << "04:40";
        else cout << "05:50";
    } else if (hh == 5) {
        if (mm < 50) cout << "05:50";
        else cout << "10:01";
    } else if (hh >= 6 && hh <= 9) {
        cout << "10:01";
    } else if (hh == 10) {
        if (mm == 0) cout << "10:01";
        else cout << "11:11";
    } else if (hh == 11) {
        if (mm < 11) cout << "11:11";
        else cout << "12:21";
    } else if (hh == 12) {
        if (mm < 21) cout << "12:21";
        else cout << "13:31";
    } else if (hh == 13) {
        if (mm < 31) cout << "13:31";
        else cout << "14:41";
    } else if (hh == 14) {
        if (mm < 41) cout << "14:41";
        else cout << "15:51";
    } else if (hh == 15) {
        if (mm < 51) cout << "15:51";
        else cout << "20:02";
    } else if (hh >= 16 && hh <= 19) {
        cout << "20:02";
    } else if (hh == 20) {
        if (mm < 2) cout << "20:02";
        else cout << "21:12";
    } else if (hh == 21) {
        if (mm < 12) cout << "21:12";
        else cout << "22:22";
    } else if (hh == 22) {
        if (mm < 22) cout << "22:22";
        else cout << "23:32";
    } else if (hh == 23) {
        if (mm < 32) cout << "23:32";
        else cout << "00:00";
    }
}

// Driver code
int main() {
    int hh = 4, mm = 45;
    findNextPalindromicTime(hh, mm);
    return 0;
}

Output:

05:50
Author: Mithlesh Upadhyay

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.