Given a number K of length N, the task is to find the smallest possible number that can be formed from K of N digits by swapping the digits any number of times.
Input: N = 15, K = 325343273113434
The smallest number possible after swapping the digits of the given number is 112233333344457
Input: N = 7, K = 3416781
Approach: The idea is to use Hashing. To implement the hash, an array arr of size 10 is created. The given number is iterated and the count of occurrence of every digit is stored in the hash at the corresponding index. Then iterate the hash array and print the ith digit according to its frequency. The output will be the smallest required number of N digits.
Below is the implementation of the above approach:
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.
- Find smallest number with given number of digits and sum of digits under given constraints
- Minimum digits to be removed to make either all digits or alternating digits same
- Find smallest number with given number of digits and sum of digits
- Multiply large integers under large modulo
- Smallest number with given sum of digits and sum of square of digits
- Count of distinct numbers formed by shuffling the digits of a large number N
- Minimum number of digits to be removed so that no two consecutive digits are same
- Given a large number, check if a subsequence of digits is divisible by 8
- Smallest number containing all possible N length permutations using digits 0 to D
- Smallest N digit number divisible by all possible prime digits
- Given count of digits 1, 2, 3, 4, find the maximum sum possible
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- How to store a very large number of more than 100 digits in C++
- Find the smallest number whose digits multiply to a given number n
- Find smallest number formed by inverting digits of given number N
- Count of numbers with all digits same in a given range
- Find the Largest number with given number of digits and sum of digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Find the smallest positive number which can not be represented by given digits
- Find the average of k digits from the beginning and l digits from the end of the given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.