text: Largest palindromic number by permuting digits | C # program to print the largest palindromic number by permuting digits of a number ; Function to check if a number can be permuted to form a palindrome number ; Counts the occurrence of number which is odd ; If occurrence is odd ; If number exceeds 1 ; Function to print the largest palindromic number by permuting digits of a number ; string length ; Map that marks the occurrence of a number ; Check the possibility of a palindromic number ; string array that stores the largest permuted palindromic number ; Pointer of front ; Greedily start from 9 to 0 and place the greater number in front and odd in the middle ; If the occurrence of number is odd ; Place one odd occurring number in the middle ; Decrease the count ; Place the rest of numbers greedily ; If all numbers occur even times , then place greedily ; Place greedily at front ; 2 numbers are placed , so decrease the count ; Increase placing position ; Print the largest string thus formed ; Driver Code
code: using System ; using System . Collections . Generic ; class GFG { static bool possibility ( Dictionary < int , int > m , int length , string s ) { int countodd = 0 ; for ( int i = 0 ; i < length ; i ++ ) { if ( ( m [ s [ i ] - '0' ] & 1 ) != 0 ) countodd ++ ; if ( countodd > 1 ) return false ; } return true ; } static void largestPalindrome ( string s ) { int l = s . Length ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < 10 ; i ++ ) m [ i ] = 0 ; for ( int i = 0 ; i < l ; i ++ ) m [ s [ i ] - '0' ] ++ ; if ( possibility ( m , l , s ) == false ) { Console . Write ( " Palindrome ▁ cannot ▁ be ▁ formed " ) ; return ; } char [ ] largest = new char [ l ] ; int front = 0 ; for ( int i = 9 ; i >= 0 ; i -- ) { if ( ( m [ i ] & 1 ) != 0 ) { largest [ l / 2 ] = ( char ) ( i + '0' ) ; m [ i ] -- ; while ( m [ i ] > 0 ) { largest [ front ] = ( char ) ( i + '0' ) ; largest [ l - front - 1 ] = ( char ) ( i + '0' ) ; m [ i ] -= 2 ; front ++ ; } } else { while ( m [ i ] > 0 ) { largest [ front ] = ( char ) ( i + '0' ) ; largest [ l - front - 1 ] = ( char ) ( i + '0' ) ; m [ i ] -= 2 ; front ++ ; } } } for ( int i = 0 ; i < l ; i ++ ) { Console . Write ( largest [ i ] ) ; } } public static void Main ( string [ ] args ) { string s = "313551" ; largestPalindrome ( s ) ; } }