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
avijit99: 2020-01-17 14:26:43

#include<iostream>
using namespace std;
int palindrom(int x);

int main()
{
long long int t,n;
cin>>t;
while(t--)
{
cin>>n;
for(;;)
{

if(palindrom(++n))
{
cout<<n<<endl;
break;
}
}
}
return 0;
}
int palindrom(int x)
{
int a[8],i=0,j,s=x;
while(x>0)
{
a[i]=x%10;
x=x/10;
i++;
}
for(j=0;j<i;j++)
{
x=(x*10)+a[j];
}
if(s==x)
{
return 1;
}
else{
return 0;
}
}
this is my code
but doesn't run

atulav: 2020-01-12 12:10:03

Finally did it after years.

godmatter_1996: 2019-12-24 10:24:06

t=int(input("Enter no of test cases"))

while t>0:
x=int(input("Enter value of K"))
if x <= 1000000 and x>=10:
x+=1
while True:
y=str(x)
z=y[::-1]
if z==y:
break
else:
x+=1


else:
print('K is crossing threshold or K is less than 10')

print(y)
t -= 1

I CODED IT LIKE THIS BUT ITS SHOWING ERROR. CAN ANYONE TELL ME WHERE HAVE I GONE WRONG? CODE WORKS FINE IN PYCHARM BUT HERE IT IS SHOWING ERROR

Last edit: 2019-12-24 10:25:24
sayan_244: 2019-12-19 07:11:03

LOL! can the input number have leading zeroes??
I am having a hard time getting rid of them.... :/

bohot_hard_123: 2019-12-04 14:19:43

why this code is not working


import java.io.*;
public class Main{
public static long reverse(long n ) {

long remainder, reversedN = 0;
try {
while (n != 0) {
remainder = n % 10;
reversedN = reversedN * 10 + remainder;
n /= 10;
}
}catch(Exception e) {
e.printStackTrace();
}
return reversedN;
}
public static void main(String[] args)throws java.lang.Exception , IOException {
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
long t = 0;
try {
t = Integer.parseInt(in.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (t-- > 0) {

long input = 0;
try {
input = Integer.parseInt(in.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true) {
input++;
if(reverse(input) == input) {
System.out.println(input);

break;
}
}

}
}
}

even in eclipse it is giving fine output and here causing wrong answer , help please

ashwanitr001: 2019-12-01 12:14:56

For people getting WA try test cases like 0,99,199
The outputs should be 1,101,202 respectively

harindra2002: 2019-11-23 14:10:06

should we input the values given in the example or leave the input to the computer?

aneeshs_7901: 2019-11-19 16:19:14

its giving wrong answer everytime i submit.its working fine and the output is correct in ideone

binit_54: 2019-10-15 06:22:42

can someone give me some idea on how to store the strings in this problem for later access when all the input has been finished?

pranay19263: 2019-10-10 08:19:56

giving runtime error all the time
beside it is fully working on ideone


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%
402 14