Skip to content
Related Articles

Related Articles

Count of numbers from range [L, R] whose sum of digits is Y using Dynamic Programming
  • Difficulty Level : Medium
  • Last Updated : 20 Apr, 2021

Pre-requisites: Recursion, Dynamic Programming, Digit DP

Given an integer Y and a range [L, R], the task is to find the count of all numbers from the given range whose sum of digits is equal to Y.
Examples: 
 

Input: L = 0, R = 11, Y = 2 
Output:
2 -> 2 
11 -> 1 + 1 = 2
Input: L = 500, R = 1000, Y = 6 
Output:
 

 

Constraints: 



0 <= L <= R <= 10^18

0 <= Y <= 162 (Which is the maximum possible sum a 18 digit number can have)

 

Approach: Firstly generalizing the question, [L, R] can be written as [0, R] – [0, L-1], i.e. first finding all the numbers in the range[0, R] whose digit sum = Y, and then for the range[0, L-1], then finally subtracting each values to get out desired result. So for the case of digit let’s store the digits of number L and R in 2 separate vectors so that accessing its digit would be easier. Then we need a function that will carry (current_index, flag, sum). This function is the main part of our logic. Initializing current_index = 0, flag = 0, sum = 0.

Let’s say R = 462, this number has 3 indexes i.e. 0, 1, 2. Now check in how many ways can you fill the 0th index. On observation we can say we can fill 0th index as 0, 1, 2, 3 and 4. If we exceed 4, then we would form a number greater than 462.

Now, if you have inserted 0 in 0th index then what can be the possibility for 1st index ? Answer – 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Since you have a number 0 in the 0th index you can fill any number from 0-9 in the 1st index. Okay as you can fill any number in next index, you will turn flag = 1. flag = 1 tell us that we have the advantage to fill anything from 0-9. Similarly thinking up for the other indexes.

Now if we see the base condition

if(current_index == n) {
if(sum == Y) return 1;
else return 0;
}

Below is the implementation of the above approach: 
 

C++




#include<bits/stdc++.h>
using namespace std;
 
// function to convert digit to vector
vector<int> digitToVec(int n) {
    vector<int> a;
    while (n) {
        a.push_back(n % 10);
        n = n / 10;
    }
    reverse(a.begin(), a.end());
    return a;
}
 
int Y;    // setting Y as global
int dp[19][2][18 * 9 + 1];    // 3D dp
int func(int ind, int flag, int sum, vector<int> a) {
    if (ind == a.size()) {
        if (sum == Y) return 1;
        else return 0;
    }
    if (dp[ind][flag][sum] != -1) return dp[ind][flag][sum];
     
      // if flag = 0, I know I can only fill from 0 to a[ind]
    // if flag = 1, I have the advantage to fill from 0 to 9
    int limit = 9;
    if (flag == 0) limit = a[ind];
 
    int cnt = 0;
    for (int num = 0; num <= limit; num++) {
          // if flag = 0, which means no advantage
          // and I am still filling the same number as a[ind] means giving him no advantage
          // hence the next recursion call flag still stays as 0
        if (flag == 0 && num == a[ind]) {
            cnt += func(ind + 1, 0, sum + num, a);
        }
        else {
            cnt += func(ind + 1, 1, sum + num, a);
        }
    }
    return dp[ind][flag][sum] = cnt;
}
 
// intermediate function helping to initialize all values of func()
int ans(int n) {
    vector<int> a = digitToVec(n);
    memset(dp, -1, sizeof dp);    // initializing dp as -1
    return func(0, 0, 0, a);
}
 
// Driver code
int main() {
    int l, r;
    cin >> l >> r >> Y;
    cout << ans(r) - ans(l - 1);
    return 0;
}
Output: 
2

 

Now talking of the worst case Time Complexity: O(19 * 2 * (18 * 9) * 10)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :