Sphere Online Judge

SPOJ Problem Set (classical)

5. The Next Palindrome

Problem code: PALIN

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

Added by:Adrian Kosowski
Time limit:2s-9s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages:All except: NODEJS PERL 6

hide comments
2014-12-18 02:39:53 Dan Pallas
One thing that isn't really part of the problem that caused me some issues. The last (at least I assume it was the last) input doesn't have a newline character in it. Don't assume that all inputs do.
2014-12-17 10:05:05 Avinash Raj
Why I'm getting w.a in SPOJ please someone help.
2014-12-15 19:58:49 Louis Burke
@naman gupta (and others)
Note that its integer k not more than 1000000 **DIGITS** so any c-derived 'int' type will not be big enough. Also implementing full arithmetic for bignums is overkill. This can be done by string manipulation.
2014-12-14 08:32:34 kapil
I have stored the numbers in strings.Can this be reason of showing wrong answer...??
2014-12-13 17:33:03 Pinki
Has anybody implemented this problem in java?? With BigInteger time limit is exceeding.
2014-12-09 19:12:15 Manglesh Sharma
what is expected in case where the integer has more than 1000000 digits

a test case:
integer more than 1000000 digits

what should be the output???
2014-12-09 18:07:54 numerix
@Kushagra Bhatnagar: Why do you post your comment here, though it was a problem with your submissions for FIBOSUM?
Explanation: You used Python 3.4 as language, but FIBOSUM is still on Pyramid cluster where Python 3.4 and PyPy are not available. Use Python 3.2 instead.
2014-12-09 15:11:40 Kushagra Bhatnagar
My Code Is running Fine In Python
But Here It Says INTERNAL ERROR what does that mean?
2014-12-08 15:16:38 naman gupta
cant find any mistake
passed tons of correct cases still saying wrong answer.
anyone help.
2014-12-06 23:58:21 Dawid
Hi guys, why does it say "compilation problem" while ideone runs it smooth? (C++).
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.