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 the largest number that can be formed by changing at most K digits
- Find Nth even length palindromic number formed using digits X and Y
- Find all strings formed from characters mapped to digits of a number
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find the number in a range having maximum product of the digits
- Maximum number formed from array with K number of adjacent swaps allowed
- Greatest number less than equal to B that can be formed from the digits of A
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Recursive sum of digits of a number formed by repeated appends
- Find smallest number with given number of digits and sum of digits under given constraints
- 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 number of teams that can be formed with given persons
- Find smallest possible Number from a given large Number with same count of digits