# Find the kth smallest number with sum of digits as m

Given two integers **M** and **K**, the task is to find the **Kth** smallest number with digit sum as **M**.

**Examples:**

Input:M = 5, K = 3

Output:23

Sequence of numbers starting from 1 with digit sum as 5 is as follows:

5

14

23

32

41

So 3rd smallest number is 23

Input:M = 4, K = 1

Output:4

**Approach:** We need to find the sequence of numbers starting from 1 with the sum of digits as m.

- Write a recursive function that will increase the numbers until the sum of the digits of the number will be equal to our required sum
**M**. - For that, start from
**0**always and check for single digit number that will add upto**M** - Let’s take an example for sum = 5 so we will start from 0 and go upto 5 in single digit as 6 exceeds out required sum
- Now from 5 we will move to two digits number 10 and we will go up to 14 because the sum of digits of 14 is 5 and 15 will exceed the required sum and so on, then further we will move in 20’s and this goes on up to 50 because after 50 till 99 the sum of digits of every number will be greater than 5 so we will skip that.
- Now we will move in three digits 100, 101, 102, … and similarly this operation will be going on until the sum of digits of the number are equal to 15.
- We will keep insert the elements into set whose sum of digits is equals to
**M**.

Below is the code for recursion:

for(int i=0;i<=min(left, 9LL);i++){ dfs(num*10+i, left-i, ct+1); }

To find kth smallest we need to sort the sequence, so it will be better if we store the numbers in a set in C++, as numbers in a set are arranged in sorted order.

**Note** that this approach will not work for bigger input values.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define int long long ` `const` `int` `N = 2005; ` ` ` `set<` `int` `> ans; ` ` ` `// Recursively moving to add ` `// all the numbers upto a limit ` `// with sum of digits as m ` `void` `dfs(` `int` `num, ` `int` `left, ` `int` `ct) ` `{ ` ` ` `// Max number of digits allowed in ` ` ` `// a number for this implementation ` ` ` `if` `(ct >= 15) ` ` ` `return` `; ` ` ` `if` `(left == 0) ` ` ` `ans.insert(num); ` ` ` `for` `(` `int` `i = 0; i <= min(left, 9LL); i++) ` ` ` `dfs(num * 10 + i, left - i, ct + 1); ` `} ` ` ` `// Function to return the kth number ` `// with sum of digits as m ` `int` `getKthNum(` `int` `m, ` `int` `k) ` `{ ` ` ` `dfs(0, m, 0); ` ` ` ` ` `int` `ct = 0; ` ` ` `for` `(` `auto` `it : ans) { ` ` ` `ct++; ` ` ` ` ` `// The kth smallest number is found ` ` ` `if` `(ct == k) { ` ` ` `return` `it; ` ` ` `} ` ` ` `} ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `m = 5, k = 3; ` ` ` ` ` `cout << getKthNum(m, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `N = ` `2005` `; ` ` ` `static` `Set<Integer> ans = ` `new` `LinkedHashSet<Integer>(); ` ` ` `// Recursively moving to add ` `// all the numbers upto a limit ` `// with sum of digits as m ` `static` `void` `dfs(` `int` `num, ` `int` `left, ` `int` `ct) ` `{ ` ` ` `// Max number of digits allowed in ` ` ` `// a number for this implementation ` ` ` `if` `(ct >= ` `15` `) ` ` ` `return` `; ` ` ` `if` `(left == ` `0` `) ` ` ` `ans.add(num); ` ` ` `for` `(` `int` `i = ` `0` `; i <= Math.min(left, ` `9` `); i++) ` ` ` `dfs(num * ` `10` `+ i, left - i, ct + ` `1` `); ` `} ` ` ` `// Function to return the kth number ` `// with sum of digits as m ` `static` `int` `getKthNum(` `int` `m, ` `int` `k) ` `{ ` ` ` `dfs(` `0` `, m, ` `0` `); ` ` ` ` ` `int` `ct = ` `0` `; ` ` ` `for` `(` `int` `it : ans) ` ` ` `{ ` ` ` `ct++; ` ` ` ` ` `// The kth smallest number is found ` ` ` `if` `(ct == k) ` ` ` `{ ` ` ` `return` `it; ` ` ` `} ` ` ` `} ` ` ` `return` `-` `1` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `m = ` `5` `, k = ` `3` `; ` ` ` ` ` `System.out.println(getKthNum(m, k)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` `static` `int` `N = 2005; ` ` ` `static` `HashSet<` `int` `> ans = ` `new` `HashSet<` `int` `>(); ` ` ` `// Recursively moving to add ` `// all the numbers upto a limit ` `// with sum of digits as m ` `static` `void` `dfs(` `int` `num, ` `int` `left, ` `int` `ct) ` `{ ` ` ` `// Max number of digits allowed in ` ` ` `// a number for this implementation ` ` ` `if` `(ct >= 15) ` ` ` `return` `; ` ` ` `if` `(left == 0) ` ` ` `ans.Add(num); ` ` ` `for` `(` `int` `i = 0; ` ` ` `i <= Math.Min(left, 9); i++) ` ` ` `dfs(num * 10 + i, left - i, ct + 1); ` `} ` ` ` `// Function to return the kth number ` `// with sum of digits as m ` `static` `int` `getKthNum(` `int` `m, ` `int` `k) ` `{ ` ` ` `dfs(0, m, 0); ` ` ` ` ` `int` `ct = 0; ` ` ` `foreach` `(` `int` `it ` `in` `ans) ` ` ` `{ ` ` ` `ct++; ` ` ` ` ` `// The kth smallest number is found ` ` ` `if` `(ct == k) ` ` ` `{ ` ` ` `return` `it; ` ` ` `} ` ` ` `} ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `m = 5, k = 3; ` ` ` ` ` `Console.WriteLine(getKthNum(m, k)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

23

## Recommended Posts:

- Find the smallest positive number which can not be represented by given digits
- Find the smallest number whose digits multiply to a given number n
- Smallest odd number with N digits
- Smallest Even number with N digits
- 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 with at least n digits in factorial
- Smallest number k such that the product of digits of k is equal to n
- Smallest number by rearranging digits of a given number
- Immediate smallest number after re-arranging the digits of a given number
- Find the average of k digits from the beginning and l digits from the end of the given number
- Find the Largest number with given number of digits and sum of digits
- Find smallest number K such that K % p = 0 and q % K = 0
- Find count of digits in a number that divide the 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 contribute@geeksforgeeks.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.