This is my code for problem SSEC0004 (arrange the dates) .My code is passing the sample test case but not the pretests.I am getting wrong answer can anyone tell what's wrong in the code

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

#define ll long long int

void solve()
{
int n,in,in1;
cin >> n;
string ar[n],br[n],str,cr[n],dr[n];
for(int i=0;i<n;i++)
cin >> ar[i];
for(int i=0;i<n;i++)
{
in=ar[i].find_last_of(’/’);
br[i]=ar[i].substr(in+1,4);
}
for(int i=0;i<n;i++)
{
in=ar[i].find_first_of(’/’);
in1=ar[i].find_last_of(’/’);
cr[i]=ar[i].substr(in+1,in1-in-1);
}
for(int i=0;i<n;i++)
{
in=ar[i].find_first_of(’/’);
dr[i]=ar[i].substr(0,in);
}
for(int i=0;i<(n-1);i++)
{
for(int j=i+1;j<n;j++)
{
if(br[i]>br[j])
swap(ar[i],ar[j]);
else if(br[i]==br[j] && cr[i]>cr[j])
swap(ar[i],ar[i+1]);
else if(br[i]==br[j] && cr[i]==cr[j] && dr[i]>dr[j])
swap(ar[i],ar[i+1]);
}
}
for(int i=0;i<n;i++)
{
cout << ar[i] << “\n”;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
/ll tc;
cin >> tc;
while(tc–)
/
solve();
}

Try this tc :-
5
1/1/2
2/2/12
1/2/3
2/1/4
1/2/5

Ans :-
1/1/2
1/2/3
2/1/4
1/2/5
2/2/12
you are comparing strings just like numbers which is wrong.
In string :- “2” > “12” as it compares character by character i.e, ‘2’ with ‘1’ of “12” hence ‘2’ > ‘1’
if its integer type :- 2 < 12
sorting way is wrong as you are swapping whole date but the initial calculated date ,month ,year position is not updated .
Try to use custom sort.
Hope this helps .!!

2 Likes

yes, i understood.thank you for helping