# Largest palindromic number by permuting digits

Given N(very large), 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.

Examples :

```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 : Pallindrome cannot be formed
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 :

## C++

 `// CPP program to print the largest palindromic ` `// number by permuting digits of a number ` `#include ` `using` `namespace` `std; ` `  `  `// function to check if a number can be ` `// permuted to form a palindrome number ` `bool` `possibility(unordered_map<``int``, ``int``> m, ` `                 ``int` `length, string s) ` `{ ` `    ``// counts the occurrence of number which is odd ` `    ``int` `countodd = 0; ` `    ``for` `(``int` `i = 0; i < length; i++) { ` ` `  `        ``// if occurrence is odd ` `        ``if` `(m[s[i] - ``'0'``] & 1) ` `            ``countodd++; ` `  `  `        ``// if number exceeds 1 ` `        ``if` `(countodd > 1) ` `            ``return` `false``; ` `    ``} ` `  `  `    ``return` `true``; ` `} ` `  `  `// function to print the largest palindromic number ` `// by permuting digits of a number ` `void` `largestPalindrome(string s) ` `{ ` `  `  `    ``// string length ` `    ``int` `l = s.length(); ` `  `  `    ``// map that marks the occurrence of a number ` `    ``unordered_map<``int``, ``int``> m; ` `    ``for` `(``int` `i = 0; i < l; i++) ` `        ``m[s[i] - ``'0'``]++; ` `  `  `    ``// check the possibility of a palindromic number ` `    ``if` `(possibility(m, l, s) == ``false``) { ` `        ``cout << ``"Palindrome cannot be formed"``; ` `        ``return``; ` `    ``} ` `  `  `    ``// string array that stores the largest ` `    ``// permuted palindromic number ` `    ``char` `largest[l]; ` `  `  `    ``// pointer of front ` `    ``int` `front = 0; ` `  `  `    ``// greedily start from 9 to 0 and place the ` `    ``// greater number in front and odd in the ` `    ``// middle ` `    ``for` `(``int` `i = 9; i >= 0; i--) { ` `  `  `        ``// if the occurrence of number is odd ` `        ``if` `(m[i] & 1) { ` `  `  `            ``// place one odd occurring number ` `            ``// in the middle ` `            ``largest[l / 2] = ``char``(i + 48); ` `  `  `            ``// decrease the count ` `            ``m[i]--; ` `  `  `            ``// place the rest of numbers greedily ` `            ``while` `(m[i] > 0) { ` `                ``largest[front] = ``char``(i + 48); ` `                ``largest[l - front - 1] = ``char``(i + 48); ` `                ``m[i] -= 2; ` `                ``front++; ` `            ``} ` `        ``} ` `        ``else` `{ ` ` `  `            ``// if all numbers occur even times, ` `            ``// then place greedily ` `            ``while` `(m[i] > 0) { ` `  `  `                ``// place greedily at front ` `                ``largest[front] = ``char``(i + 48); ` `                ``largest[l - front - 1] = ``char``(i + 48); ` `  `  `                ``// 2 numbers are placed, so decrease the count ` `                ``m[i] -= 2; ` `  `  `                ``// increase placing position ` `                ``front++; ` `            ``} ` `        ``} ` `    ``} ` `  `  `    ``// print the largest string thus formed ` `    ``for` `(``int` `i = 0; i < l; i++) ` `        ``cout << largest[i]; ` `} ` `  `  `// Driver Code ` `int` `main() ` `{ ` `    ``string s = ``"313551"``; ` `    ``largestPalindrome(s); ` `    ``return` `0; ` `} `

## Python3

# Python3 program to print the largest palindromic
# number by permuting digits of a number
from collections import defaultdict

# Function to check if a number can be
# permuted to form a palindrome number
def possibility(m, length, s):

# counts the occurrence of
# number which is odd
countodd = 0
for i in range(0, length):

# if occurrence is odd
if m[int(s[i])] & 1:
countodd += 1

# if number exceeds 1
if countodd > 1:
return False

return True

# Function to print the largest palindromic
# number by permuting digits of a number
def largestPalindrome(s):

# string length
l = len(s)

# map that marks the occurrence of a number
m = defaultdict(lambda:0)
for i in range(0, l):
m[int(s[i])] += 1

# check the possibility of a
# palindromic number
if possibility(m, l, s) == False:
print(“Palindrome cannot be formed”)
return

# string array that stores the largest
# permuted palindromic number
largest = [None] * l

# pointer of front
front = 0

# greedily start from 9 to 0 and place the
# greater number in front and odd in the middle
for i in range(9, -1, -1):

# if the occurrence of number is odd
if m[i] & 1:

# place one odd occurring number
# in the middle
largest[l // 2] = chr(i + 48)

# decrease the count
m[i] -= 1

# place the rest of numbers greedily
while m[i] > 0:
largest[front] = chr(i + 48)
largest[l – front – 1] = chr(i + 48)
m[i] -= 2
front += 1

else:

# if all numbers occur even times,
# then place greedily
while m[i] > 0:

# place greedily at front
largest[front] = chr(i + 48)
largest[l – front – 1] = chr(i + 48)

# 2 numbers are placed,
# so decrease the count
m[i] -= 2

# increase placing position
front += 1

# print the largest string thus formed
for i in range(0, l):
print(largest[i], end = “”)

# Driver Code
if __name__ == “__main__”:

s = “313551”
largestPalindrome(s)

# This code is contributed by Rituraj Jain

Output:

```531135
```

Time Complexity : O(n), where n is the length of string.