Search code examples
algorithmpalindrome

SPOJ PALIN programe running successfully on ideone,but showing wrong answer when i am posting it on SPOJ


A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

Input

The first line contains integer t, the number of test cases. Integers K are given in the next t lines.

Output

For each K, output the smallest palindrome larger than K.

Example

Input: 2 808 2133

Output: 818 2222

My code which is working successfully on ideone but spoj shows "Wrong ans" ,can any one suggest me or correct me where i am wrong, here is my code;

#include<stdio.h>
int ifPalindrome(int n){
     int r=0,k=0,a=n;
     while((a%10)!=0){
     r=a%10;
     k=10*k+r;
     a=a/10;
     }
  if(k==n)return 1;
return 0;   
}
int main(){
    int t,n;
    scanf("%d",&t);

    for(;t>0;t--){
       scanf("%d",&n);
       while(ifPalindrome(n)!=1){
        n++;
       }    
      if(ifPalindrome(n)==1)printf("%d\n",n);   
      }
      return 0;
  }

Solution

  • The mistake that you are making is that you are storing n in an integer datatype... the constraint says that

    K of not more than 1000000 digits (it is digits and not up to that number)

    It is impossible for an integer to store such a huge number..so you have to use string or character array or integer array to solve this particular problem... try this input:

    454646546546546546546546464646464646

    and you will realize where you are wrong