Given a number, find the next smallest palindrome larger than this number. For example, if the input number is “2 3 5 4 5”, the output should be “2 3 6 3 2”. And if the input number is “9 9 9”, the output should be “1 0 0 1”.

The input is assumed to be an array. Every entry in array represents a digit in input number. Let the array be ‘num[]’ and size of array be ‘n’

There can be three different types of inputs that need to be handled separately.
1) The input number is palindrome and has all 9s. For example “9 9 9”. Output should be “1 0 0 1”
2) The input number is not palindrome. For example “1 2 3 4”. Output should be “1 3 3 1”
3) The input number is palindrome and doesn’t have all 9s. For example “1 2 2 1”. Output should be “1 3 3 1”.

Solution for input type 1 is easy. The output contains n + 1 digits where the corner digits are 1, and all digits between corner digits are 0.

[ad type=”banner”]

Now let us first talk about input type 2 and 3. How to convert a given number to a greater palindrome? To understand the solution, let us first define the following two terms:
Left Side: The left half of given number. Left side of “1 2 3 4 5 6” is “1 2 3” and left side of “1 2 3 4 5” is “1 2”
Right Side: The right half of given number. Right side of “1 2 3 4 5 6” is “4 5 6” and right side of “1 2 3 4 5” is “4 5”
To convert to palindrome, we can either take the mirror of its left side or take mirror of its right side. However, if we take the mirror of the right side, then the palindrome so formed is not guaranteed to be next larger palindrome. So, we must take the mirror of left side and copy it to right side. But there are some cases that must be handled in different ways. See the following steps.

We will start with two indices i and j. i pointing to the two middle elements (or pointing to two elements around the middle element in case of n being odd). We one by one move i and j away from each other.

Step 1. Initially, ignore the part of left side which is same as the corresponding part of right side. For example, if the number is “8 3 4 2 2 4 6 9″, we ignore the middle four elements. i now points to element 3 and j now points to element 6.

Step 2. After step 1, following cases arise:

Case 1: Indices i & j cross the boundary.
This case occurs when the input number is palindrome. In this case, we just add 1 to the middle digit (or digits in case n is even) propagate the carry towards MSB digit of left side and simultaneously copy mirror of the left side to the right side.
For example, if the given number is “1 2 9 2 1”, we increment 9 to 10 and propagate the carry. So the number becomes “1 3 0 3 1”

Case 2: There are digits left between left side and right side which are not same. So, we just mirror the left side to the right side & try to minimize the number formed to guarantee the next smallest palindrome.
In this case, there can be two sub-cases.

2.1) Copying the left side to the right side is sufficient, we don’t need to increment any digits and the result is just mirror of left side. Following are some examples of this sub-case.
Next palindrome for “7 8 3 3 2 2″ is “7 8 3 3 8 7”
Next palindrome for “1 2 5 3 2 2″ is “1 2 5 5 2 1”
Next palindrome for “1 4 5 8 7 6 7 8 3 2 2″ is “1 4 5 8 7 6 7 8 5 4 1”
How do we check for this sub-case? All we need to check is the digit just after the ignored part in step 1. This digit is highlighted in above examples. If this digit is greater than the corresponding digit in right side digit, then copying the left side to the right side is sufficient and we don’t need to do anything else.

2.2) Copying the left side to the right side is NOT sufficient. This happens when the above defined digit of left side is smaller. Following are some examples of this case.
Next palindrome for “7 1 3 3 2 2″ is “7 1 4 4 1 7”
Next palindrome for “1 2 3 4 6 2 8″ is “1 2 3 5 3 2 1”
Next palindrome for “9 4 1 8 7 9 7 8 3 2 2″ is “9 4 1 8 8 0 8 8 1 4 9”
We handle this subcase like Case 1. We just add 1 to the middle digit (or digits in ase n is even) propagate the carry towards MSB digit of left side and simultaneously copy mirror of the left side to the right side.

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20an%20array%0Avoid%20printArray%20(int%20arr%5B%5D%2C%20int%20n)%3B%0A%20%0A%2F%2F%20A%20utility%20function%20to%20check%20if%20num%20has%20all%209s%0Aint%20AreAll9s%20(int%20num%5B%5D%2C%20int%20n%20)%3B%0A%20%0A%2F%2F%20Returns%20next%20palindrome%20of%20a%20given%20number%20num%5B%5D.%0A%2F%2F%20This%20function%20is%20for%20input%20type%202%20and%203%0Avoid%20generateNextPalindromeUtil%20(int%20num%5B%5D%2C%20int%20n%20)%0A%7B%0A%20%20%20%20%2F%2F%20find%20the%20index%20of%20mid%20digit%0A%20%20%20%20int%20mid%20%3D%20n%2F2%3B%0A%20%0A%20%20%20%20%2F%2F%20A%20bool%20variable%20to%20check%20if%20copy%20of%20left%20side%20to%20right%20is%20sufficient%20or%20not%0A%20%20%20%20bool%20leftsmaller%20%3D%20false%3B%0A%20%0A%20%20%20%20%2F%2F%20end%20of%20left%20side%20is%20always%20’mid%20-1’%0A%20%20%20%20int%20i%20%3D%20mid%20-%201%3B%0A%20%0A%20%20%20%20%2F%2F%20Begining%20of%20right%20side%20depends%20if%20n%20is%20odd%20or%20even%0A%20%20%20%20int%20j%20%3D%20(n%20%25%202)%3F%20mid%20%2B%201%20%3A%20mid%3B%0A%20%0A%20%20%20%2F%2F%20Initially%2C%20ignore%20the%20middle%20same%20digits%20%0A%20%20%20%20while%20(i%20%3E%3D%200%20%26%26%20num%5Bi%5D%20%3D%3D%20num%5Bj%5D)%0A%20%20%20%20%20%20%20%20i–%2Cj%2B%2B%3B%0A%20%0A%20%20%20%20%2F%2F%20Find%20if%20the%20middle%20digit(s)%20need%20to%20be%20incremented%20or%20not%20(or%20copying%20left%20%0A%20%20%20%20%2F%2F%20side%20is%20not%20sufficient)%0A%20%20%20%20if%20(%20i%20%3C%200%20%7C%7C%20num%5Bi%5D%20%3C%20num%5Bj%5D)%0A%20%20%20%20%20%20%20%20leftsmaller%20%3D%20true%3B%0A%20%0A%20%20%20%20%2F%2F%20Copy%20the%20mirror%20of%20left%20to%20tight%0A%20%20%20%20while%20(i%20%3E%3D%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20num%5Bj%5D%20%3D%20num%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20j%2B%2B%3B%0A%20%20%20%20%20%20%20%20i–%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Handle%20the%20case%20where%20middle%20digit(s)%20must%20be%20incremented.%20%0A%20%20%20%20%2F%2F%20This%20part%20of%20code%20is%20for%20CASE%201%20and%20CASE%202.2%0A%20%20%20%20if%20(leftsmaller%20%3D%3D%20true)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20carry%20%3D%201%3B%0A%20%20%20%20%20%20%20%20i%20%3D%20mid%20-%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20there%20are%20odd%20digits%2C%20then%20increment%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20middle%20digit%20and%20store%20the%20carry%0A%20%20%20%20%20%20%20%20if%20(n%252%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20num%5Bmid%5D%20%2B%3D%20carry%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20carry%20%3D%20num%5Bmid%5D%20%2F%2010%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20num%5Bmid%5D%20%25%3D%2010%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%20mid%20%2B%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%20mid%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Add%201%20to%20the%20rightmost%20digit%20of%20the%20left%20side%2C%20propagate%20the%20carry%20%0A%20%20%20%20%20%20%20%20%2F%2F%20towards%20MSB%20digit%20and%20simultaneously%20copying%20mirror%20of%20the%20left%20side%20%0A%20%20%20%20%20%20%20%20%2F%2F%20to%20the%20right%20side.%0A%20%20%20%20%20%20%20%20while%20(i%20%3E%3D%200)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20num%5Bi%5D%20%2B%3D%20carry%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20carry%20%3D%20num%5Bi%5D%20%2F%2010%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20num%5Bi%5D%20%25%3D%2010%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20num%5Bj%2B%2B%5D%20%3D%20num%5Bi–%5D%3B%20%2F%2F%20copy%20mirror%20to%20right%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20The%20function%20that%20prints%20next%20palindrome%20of%20a%20given%20number%20num%5B%5D%0A%2F%2F%20with%20n%20digits.%0Avoid%20generateNextPalindrome(%20int%20num%5B%5D%2C%20int%20n%20)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%0A%20%20%20%20printf(%22%5CnNext%20palindrome%20is%3A%5Cn%22)%3B%0A%20%0A%20%20%20%20%2F%2F%20Input%20type%201%3A%20All%20the%20digits%20are%209%2C%20simply%20o%2Fp%201%0A%20%20%20%20%2F%2F%20followed%20by%20n-1%200’s%20followed%20by%201.%0A%20%20%20%20if(%20AreAll9s(%20num%2C%20n%20)%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%20%221%20%22)%3B%0A%20%20%20%20%20%20%20%20for(%20i%20%3D%201%3B%20i%20%3C%20n%3B%20i%2B%2B%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%20%220%20%22%20)%3B%0A%20%20%20%20%20%20%20%20printf(%20%221%22%20)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Input%20type%202%20and%203%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20generateNextPalindromeUtil%20(%20num%2C%20n%20)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20print%20the%20result%0A%20%20%20%20%20%20%20%20printArray%20(num%2C%20n)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20check%20if%20num%20has%20all%209s%0Aint%20AreAll9s(%20int*%20num%2C%20int%20n%20)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for(%20i%20%3D%200%3B%20i%20%3C%20n%3B%20%2B%2Bi%20)%0A%20%20%20%20%20%20%20%20if(%20num%5Bi%5D%20!%3D%209%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20return%201%3B%0A%7D%0A%20%0A%2F*%20Utility%20that%20prints%20out%20an%20array%20on%20a%20line%20*%2F%0Avoid%20printArray(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20arr%5Bi%5D)%3B%0A%20%20%20%20printf(%22%5Cn%22)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20Program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20num%5B%5D%20%3D%20%7B9%2C%204%2C%201%2C%208%2C%207%2C%209%2C%207%2C%208%2C%203%2C%202%2C%202%7D%3B%0A%20%0A%20%20%20%20int%20n%20%3D%20sizeof%20(num)%2F%20sizeof(num%5B0%5D)%3B%0A%20%0A%20%20%20%20generateNextPalindrome(%20num%2C%20n%20)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”c program” highlight=”” provider=”manual”/]

Output:

Next palindrome is:
9 4 1 8 8 0 8 8 1 4 9
[ad type=”banner”]