Solution of the Next Palindrome (PALIN)

import java.util.*;

class Main
{
public static void main(String args[])
{

Scanner sc = new Scanner(System.in);
int number=sc.nextInt();
int a[]=new int[1000000];

int i=0;
do
{
a[i]=number%10;
number=number/10;
i++;
}while(number!=0);

int k=i;
int c=i/2;;
for(int j=0;j<(i/2);j++)
{ if(a[i-j-1]==a[j])
{ c–; }
int t=a[i-j-1];
a[i-j-1]=a[j];
a[j]=t;

}

int l1=k;
if(a[0]!=a[k-1])
{
a[k-1]=a[0];
}

int l2=l1;

if(l2%2!=0&&c==0)
{
a[l2/2]++;
}

l1-=1;

for(int l=1;l<(l1-1);l++)
{
if(a[l]!=a[l1-l])

{
if(a[l]<a[l1-l]&&l==((l1-1)/2))
{
a[l]++;

a[l2/2]=0;
}
a[l1-l]=a[l];
}

}

System.out.println();

for(int j=0;j<l2;j++)
{
System.out.print(a[j]);

}

}

}

Ive tried this code with most of the combinations and it works(some users had posted some test values in the comment section , it gives the right with those values too)…however im still getting a wrong answer !..can someone tell me whats wrong …or give me some unusual test cases to test my code with please!regards!

Hey samy21 you have to take multiple inputs as given(See I/P & O/P).You are taking only 1 value…Here judge will first enter for how many values does he want the next palindrome…then n line continues and each line have 1 integer.

Also the number can be 1000000 digits long that doesn’t fit in your int.
There’s other way round.Hope you can carry on from now.

Please read the constraint carefully.

HAPPY CODING :slight_smile:

1 Like

@samy21 : consider the test case 34567 , your code gives answer 35053 , but 34643 is smaller than your answer , bigger than the given number , and is also a palindrome . So you have a test case to debug . Best of luck .

2 Likes

The Video Solution Of The Next Palindrome - (PALIN) can be found at: How to solve the The Next Palindrome | PALIN | Codechef | Competitive coding | C++ - YouTube