The code gives wrong answer in online compiler. WHy??

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Example program
#include <iostream>
using namespace std;
int main()
{   
    int palin(long);
    long t,ret; long n;
    cin>>t;
    long x[t];
    for(long i=0;i<t;i++)
    {
        cin>>x[i];
    }
    cout<<endl;
    for(long j=0;j<t;j++)
    {
        n=x[j]+1;
        pll: ret=palin(n);
             if (ret==1)
             {
                cout<<n<<endl;
                goto nxt;
             }
             else 
             {
                 n=n+1;
                 goto pll;
             }
             nxt: ;
    }
}
int palin(long num)
{
    long t=num,f=num,dig=1,rem,ne=0;
    while(f>=10)
    {
        dig=dig*10;
        f=f/10;
    }
    while(num>0)
    {
        rem=num%10;
        ne=(rem*dig)+ne;
        num=num/10;
        dig=dig/10;
    }
    if(t==ne)
    return 1;
    else 
    return 0;
}
        
What online compiler did you use and what was the input and output? When I run your program with the example input listed above I get the output listed above too so it seems to work at least for that case.
For a given positive integer K of not more than 1000000 digits
This means K can be up to 999999 digits long. You are reading the numbers into an array of long, which can store less than 25 digits.

Get the code working as it is for the sample input. Then switch to something different for the larger input.

You will find that for large numbers, your algorithm will be too slow. Imagine a 100 digit number: you won't be able to count up by ones to find the next palindrome. You may find that strings actually work better since you're really only concerned with the individual digits and not the value of the number as a whole.

@Dhayden-- But still how can we compare the numbers greater than 25 digits. as the code says
:
write the value of the smallest palindrome larger than K to output.


How can we compare the numbers when there is no data type large enough for it ?
As I hinted, I think you need to store the numbers as strings instead. Then the problem becomes figuring out how to create the proper palindrome of digits.
Topic archived. No new replies allowed.