Given N(very large), the task if to print the largest palindromic number obtained by permuting the digits of N. If it is not possible to make a palindromic number, then print an appropriate message.
Input : 313551 Output : 531135 Explanations : 531135 is the largest number which is a palindrome, 135531, 315513 and other numbers can also be formed but we need the highest of all of the palindromes. Input : 331 Output : 313 Input : 3444 Output : Palindrome cannot be formed
Naive Approach: The naive approach will be to try all the permutations possible, and print the largest of such combinations which is a palindrome.
Efficient Approach: An efficient approach will be to use Greedy algorithm. Since the number is large, store the number in a string. Store the count of occurrences of every digit in the given number in a map. Check if it is possible to form a palindrome or not. If the digits of the given number can be rearranged to form a palindrome, then apply the greedy approach to obtain the number. Check for the occurrence of every digit (9 to 0), and place every available digit at front and back. Initially, the front pointer will be at index 0, as the largest digit will be placed at first to make the number a large one. With every step, move the front pointer 1 position ahead. If the digit occurs an odd number of times, then place one digit in the middle and rest of the even number of digits at front and back. Keep repeating the process (map[digit]/2) number of times for a single digit. After placing a particular digit which occurs an even number of times at the front and back, move the front pointer one step ahead. The placing is done till map[digit] is 0. The char array will have the largest palindromic number possible after completion of the placing of digits greedily.
In the worst case, the time complexity will be O(10 * (length of string/2)), in case the number consists of a same digit at every position.
Below is the implementation of the above idea :
Time Complexity: O(n), where n is the length of the string.
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.
- Check if B can be formed by permuting the binary digits of A
- Find the Largest number with given number of digits and sum of digits
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum digits to be removed to make either all digits or alternating digits same
- Form the largest palindromic number using atmost two swaps
- Largest palindromic number in an array
- Next higher palindromic number using the same set of digits
- Find Nth even length palindromic number formed using digits X and Y
- Largest palindromic prime in an array
- Find the lexicographically largest palindromic Subsequence of a String
- Largest palindromic string possible from given strings by rearranging the characters
- Find smallest 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 Largest Cube formed by Deleting minimum Digits from a number
- Largest number not greater than N which can become prime after rearranging its digits
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Make largest palindrome by changing at most K-digits
- Smallest and Largest Palindrome with N Digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.