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. #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 <= 9999def 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