Given a palindromic number num having n number of digits. The problem is to find the smallest palindromic number greater than num using the same set of digits as in num. If no such number can be formed then print “Not Possible”.
The number could be very large and may or may not even fit into long long int.
Input : 4697557964 Output : 4756996574 Input : 543212345 Output : Not Possible
Approach: Following are the steps:
- If number of digits n <= 3, then print "Not Possible" and return.
- Calculate mid = n/2 – 1.
- Start traversing from the digit at index mid up to the 1st digit and while traversing find the index i of the rightmost digit which is smaller than the digit on its right side.
- Now search for the smallest digit greater than the digit num[i] in the index range i+1 to mid. Let the index of this digit be smallest.
- If no such smallest digit found, then print “Not Possible”.
- Else the swap the digits at index i and smallest and also swap the digits at index n-i-1 and n-smallest-1. This step is done so as to maintain the palindromic property in num.
- Now reverse the digits in the index range i+1 to mid. Also If n is even then reverse the digits in the index range mid+1 to n-i-2 else if n is odd then reverse the digits in the index range mid+2 to n-i-2. This step is done so as to maintain the palindromic property in num.
- Print the final modified number num.
Next Palindrome: 4756996574
Time Complexity: O(n)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Largest palindromic number by permuting digits
- Find Nth even length palindromic number formed using digits X and Y
- Next higher number using atmost one swap operation
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Find smallest number with given number of digits and sum of digits under given constraints
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find the average of k digits from the beginning and l digits from the end of the given number
- Minimum number of digits to be removed so that no two consecutive digits are same
- Check if the sum of digits of number is divisible by all of its digits
- Smallest number with given sum of digits and sum of square of digits
- Make palindromic string non-palindromic by rearranging its letters
- Palindromic divisors of a number
- Number of palindromic permutations | Set 1
- Longest Palindromic Substring using Palindromic Tree | Set 3