Given a number, write a program to find a maximum number that can be formed using all of the digits of this number.
Input : 38293367 Output : 98763332 Input : 1203465 Output: 6543210
Simple Approach: The simple method to solve this problem is to extract and store the digits of the given number in an integer array and sort this array in descending order. After sorting the array, print the elements of the array.
Time Complexity: O( N log N ), where N is the number of digits in the given number.
Efficient approach : We know that the digits in a number will range from 0-9, so the idea is to create a hashed array of size 10 and store the count of every digit in the hashed array that occurs in the number. Then traverse the hashed array from index 9 to 0 and calculate the number accordingly.
Below is the implementation of above efficient approach:
Time Complexity: O( N ), where N is the number of digits in the given number.
Note: For very large numbers we can use strings to take input instead of storing input in integer data type.
This article is contributed by Rohit Thapliyal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find all strings formed from characters mapped to digits of a number
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Maximum number formed from array with K number of adjacent swaps allowed
- Find the number in a range having maximum product of the digits
- Greatest number less than equal to B that can be formed from the digits of A
- Recursive sum of digits of a number formed by repeated appends
- Maximum number of teams that can be formed with given persons
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Maximum possible time that can be formed from four digits
- Find count of digits in a number that divide the number
- Find the smallest number whose digits multiply to a given number n
- Find M-th number whose repeated sum of digits of a number is N
- Maximum sum and product of the M consecutive digits in a number
- Divide a number into two parts such that sum of digits is maximum