Given a Integer, find the maximum number that can be formed from the digits. Input : 8754365 output : 8765543
I told solution in $O(n logn)$. He asked me optimize further.
We can use Hash Table to optimize further, $\rightarrow$ O(n)
Algorithm: 1. Take a hash table with size 10 (0 to 9). 2. Store the count of every digit from 0 to 9. 3. Print the index of the Hash table with respect to digit count in the reverse direction (from 9 to 0).
Example:
Hash table after digit count for 8754365 $\rightarrow$ (0 0 0 1 1 2 1 1 1 0) Print the index of the hash table with respect to their count in reverse order $\rightarrow$ 8765543 Time Complexity : O(n) Correct me if I am wrong.