Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth A special date problem solution

YASH PAL, 31 July 2024
In this HackerEarth, A special date problem solution A special birth date is D. Your task is to find the last palindrome date that comes before a date D. 
You are given a date D in the string format. The date format is as follows:
A valid date D is of length 8 and in the DDMMYYYY format.
A valid date ranges between 01 to 30.
A valid month ranges between 01 to 12.
A valid year ranges between 0001 to 9999.
HackerEarth A special date problem solution

HackerEarth A special date problem solution.

#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define fix fixed<<setprecision(10)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

bool ok(int ty,int tm,int td,int y,int m,int d){
if(ty>y) return 0;
if(ty<y) return 1;
if(tm>m) return 0;
if(tm<m) return 1;
return td<d;
}

void solve(){
string s;
cin>>s;
int d=(s[0]-'0')*10+(s[1]-'0');
int m=(s[2]-'0')*10+(s[3]-'0');
int y=(s[4]-'0')*1000+(s[5]-'0')*100+(s[6]-'0')*10+(s[7]-'0');
set<vi>S;
rep(i,1,30) rep(j,1,12){
int td=i,tm=j,ty=0;
ty=tm%10*1000+tm/10*100+td%10*10+td/10;
if(ok(ty,tm,td,y,m,d)){
S.insert({ty,tm,td});
}
}
if(!S.empty()){
vi u=*S.rbegin();
string ans;
ans+=char('0'+u[2]/10);
ans+=char('0'+u[2]%10);
ans+=char('0'+u[1]/10);
ans+=char('0'+u[1]%10);
ans+=char('0'+u[0]/1000);
ans+=char('0'+u[0]/100%10);
ans+=char('0'+u[0]/10%10);
ans+=char('0'+u[0]%10);
cout<<ans<<'n';
}
else cout<<"-1n";
}

signed main(){
FastIO;
int t;
cin>>t;
while(t--) solve();
return 0;
}

Second solution

t = int(input())


def is_valid(s):
d = int(s[:2])
m = int(s[2:4])
y = int(s[4:])
return 1 <= d <= 30 and 1 <= m <= 12 and 1 <= y <= 9999


def cmp(s):
return s[4:] + s[2:4] + s[:2]


while t > 0:
t -= 1
c = d = input()
d = d[7:3:-1] + d[4:8]
while True:
if is_valid(d) and cmp(d) <= cmp(c):
print(d)
break
y = int(d[4:])
if y <= 1:
print(-1)
break
y -= 1
y = f"{y:04}"
d = y[::-1] + y
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes