Given a non-negative number n and a value k. Find the kth smallest number that can be formed using the digits of the given number n. It is guaranteed that the kth smallest number can be formed. Note that the number could be very large and may not even fit into long long int.
Input : n = 1234, k = 2 Output : 1243 Input : n = 36012679802, k = 4 Output : 10022366897
The idea is to first sort digits and find the smallest number, then find k-th permutation starting from smallest number. To sort digits, we use an frequency counting technique as number of digits are small.
This article is contributed by Ayush Jauhari. 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 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.
- 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
- Smallest number with given sum of digits and sum of square of digits
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- 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
- Smallest multiple of N formed using the given set of digits
- Find the smallest number whose digits multiply to a given number n
- Smallest number by rearranging digits of a given number
- Immediate smallest number after re-arranging the digits of a given number
- 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
- Smallest odd number with even sum of digits from the 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
- Smallest number containing all possible N length permutations using digits 0 to D
- Smallest number greater than or equal to N using only digits 1 to K
- Smallest multiple of a given number made of digits 0 and 9 only
- 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