Finding the next palindrome for the given number

Hi Chandra, In Method 2, How would you handle a n…

Vinay Sarang - Sep 6, 2014

Hi Chandra,

In Method 2, How would you handle a number with odd digits.
Ex :: 31451 then output has to be 31513.
Could you please share the algo for it.

Thanks in advance,
Vinay

Keep the middle number somewhere, so the number will become, first+middle+reverse(first). Increment first until we get the right pallindrome.

Regards,
Kinshuk

This will not guarentee the immediate next palindrome right
for example 53165
as per your logic it will come 54145
but correct answer is 53235

I agree with you Abhsihek. I was wrong. We need lower of the 2 values and increment and then proceed. Thanks for the correction.


See also