Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of given number.
Input: n = 846903 Output: 304689 Input: n = 55010 Output: 10055
Steps to find the smallest number.
- Count the frequency of each digit in the number.
- Place the smallest digit (except 0) at the left most of required number.
and decrement the frequency of that digit by 1.
- Place all remaining digits in ascending order from left to right.
This solution is based on counting sort.
Another Approach:Find smallest permutation of given number
This article is contributed by Ajay Kumar Agrahari(aJy aGr). 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Largest number not greater than N which can become prime after rearranging its digits
- 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
- Largest palindromic string possible from given strings by rearranging the characters
- Length of longest common prefix possible by rearranging strings in a given array
- Smallest number with given sum of digits and sum of square of digits
- Cost of rearranging the array such that no element exceeds the sum of its adjacent elements
- Count of distinct XORs formed by rearranging two Binary strings
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find the smallest number whose digits multiply to a given number n
- Get the kth smallest number using the digits of the given number
- Immediate smallest number after re-arranging the digits of a given number
- Smallest odd number with even sum of digits from the given number N
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest number formed by inverting digits of given number N
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits