Related Articles
Get the kth smallest number using the digits of the given number
• Difficulty Level : Medium
• Last Updated : 16 Jul, 2017

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
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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 ``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

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.

My Personal Notes arrow_drop_up