PALIN - The Next Palindrome

no tags 

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


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

hide comments
Punit Bhatt: 2015-07-02 07:53:08

https://ideone.com/3SnQ9o.
I am getting wrong answer for my code . I have tested my code with many sample inputs and getting the required output. I have taken care of the case related to 9s. Can sumone please check my code and help me ? Thank You

Last edit: 2015-07-02 07:54:10
deeksha: 2015-07-01 18:31:46

my code is showing coorect output still it is showing wrong answer

Jesus E. Larios Murillo: 2015-07-01 05:51:25

Okay for those people coding in c. I have been taught that the correct way of getting input is to use fgets(buffer, buffer_size, stdin). This avoids overflows. This brings in the new line character also, so I follow it by buffer[strlen(buffer) - 1) = '\0', to get rid of it. This is what I tried to do with this problem, and I have spent HOURS trying to figure out why it doesn't work, even though everything seems right. Finally, serendipitously, I changed the fgets to scanf("%s", buffer), and the code that was not passing before is now passing. I have no words to describe this feeling. Can someone tell me why fgets doesn't work?

tl;dr: don't use fgets, user scanf("%s", buffer)

Tanuj Kumar: 2015-06-27 08:42:24

This shows WA all the time. Hours of effort for nothing.
Everything is Working. I mean EVERYTHING.
I've even taken into account the newline character not being present after the last input.
0000 -> 1
0009 -> 11
99 ->101
1234567890 ->1234664321
123456789 ->123464321
I now seriously doubt the judging criteria. I think either there are certain hidden outputs for particular inputs that we don't know about(which should have been disclosed in the problem statement itself) or this is plain bullsh**.

My code's IDEONE link: https://ideone.com/6o98od

Please do take a look at it. If you find a loophole please let me know in the comments.
I've put 6 hours on this one. I'm open to every kind of criticism and verbal abuse. I'll keep a tab on the comments here.
P.S. My code is in C.

Last edit: 2015-06-27 09:38:36
ankur_gupta54: 2015-06-26 14:10:07


Input

10
196916390160631690360196263916293618639160663066386802964312986
272073477482364826498296381969638969136431864389368643846646496
18585151575817912958367457547519415415945915419541854519145154
15614641486144818418478154751947125419577192
185814415857194791694616491649389468136868168686198
18581791975461631823686738618680361062836810263062012
916971951391535195392595196371636163293192391391698369126396
1575317515735715159972975361391469469164616461964801604100186064160846081
1954975915471545481581591541544040149123741477246828438245286486
991661946640614680016408460164862461460164014140040164064


Your Code's Output

196916390160631690360196263916414619362691063096136061093619691
272073477482364826498296381969767969183692894628463284774370272
18585151575817912958367457547788574575476385921971857515158581
15614641486144818418688481481844168414641651
185814415857194791694616636616496197491758514418581
18581791975461631823686739693768632813616457919718581
916971951391535195392595196441173691595293591535193159179619
1575317515735715159972975361391469483849641931635792799515175375157135751
1954975915471545481581591541546604451451951851845451745195794591
991661946640614680016408460222064804610086416046649166199


I am getting this output within 0.185 sec... then also I am getting TLE... can anyone help me out plzzz

Anurag Pasi: 2015-06-26 10:42:48

after 3 WA finally got green AC , there is no great feeling than doing it yourself and debugging it urself also, it sometimes get frustated to debug but when bug is fixed and got AC, then i m on top of da world :)
Very nice problem on strings traversing :)

Ishaan: 2015-06-24 18:55:56

Getting TLE when compiling the problem. Almost tested for test case my algorithm works fine. I am using python for the problem. How to minimize my time. Any suggestion will welcomed. (I am newbie in python - any helpful syntax??)

kunal: 2015-06-23 19:18:44

How to store a 100000 digit no ??

sg300691: 2015-06-22 17:07:37

getting the correct answer of my code on my pc and also on ideone

Amitayush Thakur: 2015-06-22 10:15:09

This problem requires you to take care of so many test cases. There is a possibility of error even when you check for tricky cases like 9,99,100,101 etc. Also do remember answer for single digit numbers will not be 11.
(edit)-> single digit numbers except 9.

Last edit: 2015-06-22 10:16:41

Problem's scores 1 vote

Concept difficulty
Concept difficulty 41%
Implementation difficulty
Implementation difficulty 44%
50