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:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find smallest number with given number of digits and sum of digits under given constraints
- Find the smallest number whose digits multiply to a given number n
- Find smallest number with given number of digits and sum of digits
- Find the kth smallest number with sum of digits as m
- Find count of digits in a number that divide the number
- Find the smallest positive number which can not be represented by given digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Smallest number by rearranging digits of a given number
- Immediate smallest number after re-arranging the digits of a given number
- Given a large number, check if a subsequence of digits is divisible by 8
- Smallest number with given sum of digits and sum of square of digits
- Smallest odd number with even sum of digits from the given number N
- Get the kth smallest number using the digits of the given number
- Smallest odd number with N digits
- Smallest odd digits number not less than N
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.