PALIN - The Next Palindrome

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.


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


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




Warning: large Input/Output data, be careful with certain languages

hide comments
fjkexgnffhwihp: 2022-06-20 17:09:50

Why is the implementation difficulty so high? I found it to be pretty easy in both concept and implementation in CPP14

paintbucket7: 2022-04-23 21:04:52

If you're getting SIGABRT then it means that you're probably converting the given string to int so you must not do that. You must solve the problem in the string itself as the input is too large.

Moreover, if you're getting the Wrong Answer then you should use the test cases provided by @shreyas_07 in this comment section. They're around 1-2 pages behind the current page. They would help you debug your program.

mmani0831: 2022-02-28 17:43:28

I got right answers for sample and custom test case. But still i am getting WA.

zinfx_09: 2022-02-23 03:19:25

10^6 digits not 10^6 int...

slony: 2022-01-16 20:42:52

For me, the problem with wrong answers was caused by leading zeros or spaces in input values. So assume that you must sanitize input first.

naman_kumar1: 2021-11-19 19:18:06

my code is running successfully in cmd prompt but judge is not accepting it, it is showing wrong answer. what could be the reason behind it.

lxy2000: 2021-11-19 05:58:14


shubhojeet_999: 2021-11-18 17:42:00

That was my code, can anyone please tell, what's the error ?

[NG]: Welcome to SPOJ; please read the footer.

Last edit: 2021-11-18 22:33:59
johanfire: 2021-10-13 04:45:28

I got it but it's exceding time limit, I wrote 19 lines of code :(

kemar: 2021-09-17 18:06:08

It seems that the last line of the input file doesn't end with a '\n'.
This costed me more than 10 WA !
Finally got AC in Python using a list of digits.

Added by:adrian
Time limit:2s-9s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6

Problem's scores 1 vote

Concept difficulty
Concept difficulty 37%
Implementation difficulty
Implementation difficulty 50%
462 16