Find the kth smallest number with sum of digits as m Last Updated : 16 Nov, 2022 Comments Improve Suggest changes Like Article Like Report 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 23Input: 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 MLet'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 sumNow 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. 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; } 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 Python3 # Python3 implementation of the approach # define long long N = 2005 ans = dict() # Recursively moving to add # all the numbers upto a limit # with sum of digits as m def dfs(n, left, ct): # Max number of digits allowed in # a number for this implementation if (ct >= 15): return if (left == 0): ans[n] = 1 for i in range(min(left, 9) + 1): dfs(n * 10 + i, left - i, ct + 1) # Function to return the kth number # with sum of digits as m def getKthNum(m, k): dfs(0, m, 0) c = 0 for it in sorted(ans.keys()): c += 1 # The kth smallest number is found if (c == k): return it return -1 # Driver code m = 5 k = 3 print(getKthNum(m, k)) # This code is contributed by Mohit Kumar 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 JavaScript <script> // Javascript implementation of the approach let N = 2005; let ans = new Set(); // Recursively moving to add // all the numbers upto a limit // with sum of digits as m function dfs(num,left,ct) { // Max number of digits allowed in // a number for this implementation if (ct >= 15) return; if (left == 0) ans.add(num); for (let 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 function getKthNum(m,k) { dfs(0, m, 0); let ct = 0; for (let it of ans.values()) { ct++; // The kth smallest number is found if (ct == k) { return it; } } return -1; } // Driver code let m = 5, k = 3; document.write(getKthNum(m, k)); // This code is contributed by unknown2108 </script> Output: 23 Comment More infoAdvertise with us Next Article Find the kth smallest number with sum of digits as m md1844 Follow Improve Article Tags : Misc Sorting Mathematical Recursion DSA +1 More Practice Tags : MathematicalMiscRecursionSorting Similar Reads Find the smallest number whose sum of digits is N Given a positive integers N, the task is to find the smallest number whose sum of digits is N.Example: Input: N = 10Output: 19Explanation: 1 + 9 = 10 = N Input: N = 18Output: 99Explanation: 9 + 9 = 18 = N Naive Approach: A Naive approach is to run a loop of i starting from 0 and find Sum of digits o 6 min read Find smallest number with given digits and sum of digits Given two positive integers P and Q, find the minimum integer containing only digits P and Q such that the sum of the digits of the integer is N. Example: Input: N = 11, P = 4, Q = 7 Output: 47Explanation: There are two possible integers that can be formed from 4 and 7 such that their sum is 11 i.e. 9 min read Smallest number with sum of digits as N and divisible by 10^N Find the smallest number such that the sum of its digits is N and it is divisible by 10^N . Examples : Input : N = 5 Output : 500000 500000 is the smallest number divisible by 10^5 and sum of digits as 5. Input : N = 20 Output : 29900000000000000000000Recommended PracticeSmallest number with sum of 6 min read Find the smallest number with n set and m unset bits Given two non-negative numbers n and m. The problem is to find the smallest number having n number of set bits and m number of unset bits in its binary representation.Constraints: 1 <= n, 0 <= m, (m+n) <= 31Note : 0 bits before leading 1 (or leftmost 1) in binary representation are counted 7 min read Smallest integer with digit sum M and multiple of N Given two positive integers N and M, the task is to find the smallest positive integer which is divisible by N and whose digit sum is M. Print -1 if no such integer exists within the range of int. Examples: Input: N = 13, M = 32 Output: 8879 8879 is divisible by 13 and its Sum of digits of 8879 is 8 5 min read Get the kth smallest number using the digits of the given number 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 = 12 11 min read Smallest number greater than Y with sum of digits equal to X Given two integers X and Y, find the minimal number with the sum of digits X, which is strictly greater than Y. Examples: Input: X = 18, Y = 99 Output: 189 Explanation: 189 is the smallest number greater than 99 having sum of digits = 18. Input: X = 12, Y = 72 Output: 75 Explanation: 75 is the small 11 min read Smallest number with given digit count and sum Given two integers s and d, find the smallest possible number that has exactly d digits and a sum of digits equal to s.Return the number as a string. If no such number exists, return "-1".Examples :Input: s = 9, d = 2Output: 18 Explanation: 18 is the smallest number possible with the sum of digits = 10 min read What is the smallest 4 digit number? The method to represent and work with numbers is known as the number system. A number system is a system of writing to represent numbers. It is the mathematical notation used to represent numbers of a given set by using digits or other symbols. It has arithmetic operations to perform division, multi 5 min read Find the Largest number with given number of digits and sum of digits Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d. Examples: Input: s = 9, d = 2Output: 90 Input: s = 20, d = 3Output: 992 Recommended PracticeLargest number possibleTry It! Naive Approach: Consider all m digit numbers and keep a max v 13 min read Like