1 / 6
Mar 2021

The judge says Wrong Answer for the code that I submitted for this problem3
Here’s the link for ideone3
and the code is:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>

char *nozeros(char *s){
	int i;
	for(i=0; i<strlen(s); i++){
		if(s[i] != '0')
			break;
	}
	return s+i;
}

int all9(char *s){
	for(int i=0; i<strlen(s); i++){
		if(s[i] != '9')
			return 0;
	}
	return 1;
}

void incrementFromMiddle(char *s, int pos){
	int len = strlen(s);
	for(int i=pos; i>=0; i--){
		if(s[i] != '9'){
			s[i] += 1;
			s[(len%2 == 0)?len-i-1:len-i] = s[i];
			break;
		}
		s[i] = '0';
		s[(len%2 == 0)?len-i-1:len-i] = s[i];
	}
}

int main(){
	int t;
	scanf("%i", &t);
	while(t--){
		char s[1000000];
		scanf("%s", s);
		char *ptr = nozeros(s);
		int len = strlen(ptr);
		if(all9(ptr)){
			memset(ptr, '0', len);
			ptr[0] = '1';
			ptr[len] = '1';
			ptr[len + 1] = '\0';
			printf("%s\n", ptr);
			continue;
		}
		for(int i=len-1; i>=0; i--){
			if(ptr[i] != '9'){
				ptr[i] += 1;
				break;
			}
			ptr[i] = '0';
		}
		short firstTime=1;
		short odd = len % 2;
		int midpoint = floor(len / 2);
		int start = (odd)? midpoint+1 : midpoint;
		for(int i=start; i<len; i++){
			if(ptr[i] == ptr[len - i - 1]){
				continue;
			}
			if(ptr[i] > ptr[len - i - 1] && firstTime == 1){
				if(odd)
					incrementFromMiddle(ptr, midpoint);
				else
					incrementFromMiddle(ptr, midpoint-1);
			}
			ptr[i] = ptr[len - i - 1];
			firstTime = 0;
		}
		printf("%s\n", ptr);
	}
	return 0;
}

Any help will be highly appreciated!!:tada::balloon:
Thanks.:pray:

  • created

    Mar '21
  • last reply

    Mar '21
  • 5

    replies

  • 687

    views

  • 2

    users

  • 3

    links

As I said in the other thread, what’s going to happen with
char s[1000000];
when the input is 1 million 9s?

That’s going to be 1 followed by 1 less than 1 million 0’s and 1
But it’ll exceed 1 million digit mark…
So what am I supposed to do in this case, throw an error?

1 million 9s is a valid test case, so you’re expected to give the correct answer.

Use Notepad or similar.

Enter ten 9s - it’s 10 digits long
Copy it, and paste it nine times - it’s now 100 digits
Copy it, and paste it nine times - 1000 digits
Copy it, and paste it nine times - 10000 digits
Copy it, and paste it nine times - 100000 digits
Copy it, and paste it nine times - 1000000 digits

You now have an integer with 1 million 9s. Pass this to your PALIN code, and check the answer.

Well I checked it by similar means and getting some error so need to fix the code…
Thank you for the support
[edit]**
I forgot to update that my code got accepted after fixing the bug…:confetti_ball::tada::balloon::chocolate_bar: