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.

Examples:

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.

`// C++ implementation to get the kth smallest ` `// number using the digits of the given number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to get the smallest digit in 'num' ` `// which is greater than 0 ` `char` `getSmallDgtGreaterThanZero(string num, ` `int` `n) ` `{ ` ` ` `// 's_dgt' to store the smallest digit ` ` ` `// greater than 0 ` ` ` `char` `s_dgt = ` `'9'` `; ` ` ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `if` `(num[i] < s_dgt && num[i] != ` `'0'` `) ` ` ` `s_dgt = num[i]; ` ` ` ` ` `// required smallest digit in 'num' ` ` ` `return` `s_dgt; ` `} ` ` ` `// function to get the kth smallest number ` `string kthSmallestNumber(string num, ` `int` `k) ` `{ ` ` ` `// FIND SMALLEST POSSIBLE NUMBER BY SORTING ` ` ` `// DIGITS ` ` ` ` ` `// count frequency of each digit ` ` ` `int` `freq[10]; ` ` ` `string final_num = ` `""` `; ` ` ` ` ` `memset` `(freq, 0, ` `sizeof` `(freq)); ` ` ` `int` `n = num.size(); ` ` ` ` ` `// counting frequency of each digit ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `freq[num[i] - ` `'0'` `]++; ` ` ` ` ` `// get the smallest digit greater than 0 ` ` ` `char` `s_dgt = getSmallDgtGreaterThanZero(num, n); ` ` ` ` ` `// add 's_dgt' to 'final_num' ` ` ` `final_num += s_dgt; ` ` ` ` ` `// reduce frequency of 's_dgt' by 1 in 'freq' ` ` ` `freq[s_dgt - ` `'0'` `]--; ` ` ` ` ` `// add each digit according to its frequency ` ` ` `// to 'final_num' ` ` ` `for` `(` `int` `i=0; i<10; i++) ` ` ` `for` `(` `int` `j=1; j<=freq[i]; j++) ` ` ` `final_num += (` `char` `)(i+48); ` ` ` ` ` `// FIND K-TH PERMUTATION OF SMALLEST NUMBER ` ` ` `for` `(` `int` `i=1; i<k; i++) ` ` ` `next_permutation(final_num.begin(), final_num.end()); ` ` ` ` ` `// required kth smallest number ` ` ` `return` `final_num; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `string num = ` `"36012679802"` `; ` ` ` `int` `k = 4; ` ` ` `cout << kthSmallestNumber(num, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

10022366897

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 contribute@geeksforgeeks.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.

## Recommended Posts:

- Find smallest number with given number of digits and sum of digits under given constraints
- 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 smallest number with given number of digits and sum of digits
- Find the smallest number whose digits multiply to a given number n
- Immediate smallest number after re-arranging the digits of a given number
- Smallest number by rearranging digits of a given number
- 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
- Smallest even digits number not less than N
- Smallest odd digits number not less than N
- Smallest number with sum of digits as N and divisible by 10^N
- Smallest number whose sum of digits is square of N
- Smallest number whose square has N digits
- Find the smallest number whose sum of digits is N
- Smallest multiple of a given number made of digits 0 and 9 only
- Smallest number k such that the product of digits of k is equal to n
- Smallest number greater than or equal to N using only digits 1 to K
- Smallest number containing all possible N length permutations using digits 0 to D
- Smallest number greater than Y with sum of digits equal to X