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.

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

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


hide comments
miche_atucha: 2016-01-25 04:50:25

"given positive integer K of not more than 1000000 digits", this really means a number with 1_000_000 digits????

sri: 2016-01-22 17:13:32

Solved after 1 year.....
see the algorithm in geeks learnt a new thing

psweden: 2016-01-21 00:35:41

This problem is quite simple to solve. Haskell's String ( [Char] ) is unusable for this - you'll get TLE. Use ByteString which has O(1) for important functions such as last, drop, take, length (where [Char] has O(n))

shreyans4020: 2016-01-17 15:09:04

TLE error for no good reason :(

chaze: 2016-01-10 16:13:05

1000000 digits could be a reason for NZEC runtime error.

pv: 2016-01-02 21:11:27

So, i implemented this in c++ (converted my recursive logic to an iterative one) and i still get a WA. So while trying to find reasons for why this happens, I came across a comment here saying single digit numbers are not palindromes ... is this true??
EDIT : Forgot to mention this but i have also taken care of cases like 99, 100 etc. Also, i checked on wikipedia, and it says that single digit numbers are palindromic.

Last edit: 2016-01-02 21:14:37
nithi_karu: 2016-01-02 06:38:49

Many of my java programs are rejected by Spoj one being this Next Palindrome Program.

dupletor: 2015-12-28 20:10:06

I did a correct algorithm, but it is limited to the integer received (2.1b?)
Even if I could put the 1000000 digits to an integer vector, how am I supposed to receive the number?

Last edit: 2015-12-28 20:20:56
ashu14: 2015-12-27 15:58:12

@imshubhamk it means that no cant be like 0067

imshubhamk: 2015-12-25 18:11:56

what does without leading zero mean can anybody help me please......

Last edit: 2015-12-25 18:23:23

Added by:adrian
Date:2004-05-01
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%
467 16