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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Find smallest number with given number of digits and sum of digits under given constraints
- 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
- Find the Largest number with given number of digits and sum of digits
- Find smallest possible Number from a given large Number with same count of digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Maximum number of teams that can be formed with given persons
- Find the smallest number whose digits multiply to a given number n
- Find count of digits in a number that divide the number
- Maximum number of line intersections formed through intersection of N planes
- Find M-th number whose repeated sum of digits of a number is N