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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Next higher number using atmost one swap operation
- Count of days remaining for the next day with higher temperature
- Find next greater number with same set of digits
- Minimum digits to be removed to make either all digits or alternating digits same
- Next greater Number than N with the same quantity of digits A and B
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Lexicographically next greater string using same character set
- Minimum number of digits to be removed so that no two consecutive digits are same
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Binary representation of next greater number with same number of 1's and 0's
- Find Nth even length palindromic number formed using digits X and Y
- Find Next number having distinct digits from the given number N
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Next greater number on the basis of precedence of digits
- Next Number with distinct digits
- Largest palindromic number by permuting digits
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Count non-palindromic array elements having same first and last digit