Open In App

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: 

14 
23 
32 
41 
So 3rd smallest number is 23
Input: M = 4, K = 1 
Output:
 

 

Approach: We need to find the sequence of numbers starting from 1 with the sum of digits as 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++ 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 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 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# 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




<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

 


Article Tags :