What is wrong in this solution to Pall01 problem?

Problem : PALL01 Problem - CodeChef
Although this code can be done successfully by reversing the integer digits but I tried submitting it using another approach. Can anyone tell me what is wrong with this code?

#include < iostream >
using namespace std;

int main() {
// your code goes here
int test_cases, checknum;
int t=0;
cin>>test_cases;
for(t=0;t<test_cases;t++){
cin>>checknum;
int x=0, temp, wins=0;
if(checknum>=10 && checknum<=99){
temp=checknum%10;
x=checknum/10;
if(x==temp){
wins=1;
}
}
else if(checknum>=100 && checknum<=999){
temp=checknum%10;
x=checknum/100;
if(x==temp){
wins=1;
}
}
else if(checknum>=1000 && checknum<=9999){
temp=checknum%100;
x=checknum/100;
if(x==temp){
wins=1;
}
}
else if(checknum>=1 && checknum<=9){
wins =1;
}
if(wins==1)
cout<<“wins\n”;
else
cout<<“losses\n”;
}
return 0;
}

The problem is when the input is greater than 1000.

In your code you are comparing the first half with the second half in the same direction (left to right).
i.e. if the input is something like 1010, the code throws win as 10 == 10 and 1221 becomes loss as 12 != 21.

You have to compare the two halves in the opposite directions.

1 Like

Oh yes, thank you. This is silly, I didn’t notice it. :slight_smile:

else if(checknum>=1000 && checknum<=9999){
temp=checknum%10;
x=checknum/10;
temp=temp*10 + x%10;
x=checknum/100;
if(x==temp){
wins=1;
}
}

Corrected this part but it is still not getting submitted.

Apparently the constraint given for n is wrong.
The value of n exceeds 10000 in some test cases.

CodeChef: Practical coding for everyone gives AC.
CodeChef: Practical coding for everyone gives runtime error.

I am not sure how large the input is so you should probably stick to reversing.

1 Like