Find maximum number that can be formed using digits of a given number | Function to print the maximum number ; hashed array to store count of digits ; Converting given number to string ; Updating the count array ; result is to store the final number ; Traversing the count array to calculate the maximum number ; return the result ; Driver program to test above function
< ? php function printMaxNum ( $ num ) { $ count = array_fill ( 0 , 10 , NULL ) ; $ str = ( string ) $ num ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) $ count [ ord ( $ str [ $ i ] ) - ord ( '0' ) ] ++ ; $ result = 0 ; $ multiplier = 1 ; for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { while ( $ count [ $ i ] > 0 ) { $ result = $ result + ( $ i * $ multiplier ) ; $ count [ $ i ] -- ; $ multiplier = $ multiplier * 10 ; } } return $ result ; } $ num = 38293367 ; echo printMaxNum ( $ num ) ; ? >