# Count numbers (smaller than or equal to N) with given digit sum in C++

C++Server Side ProgrammingProgramming

Given a string str containing a number and a sum total as input. The goal is to find numbers upto str that have sum of digits equal to total.

## Let us understand with examples.

For Example

Input - N=”110”  sum=5

Output - Count of numbers smaller than or equal to N with given digit sum are: 7

Explanation - The numbers upto 110 that have sum of digits equal to 5 are :-

5, 14, 23, 32, 41, 50 and 104.

Input - N=”1000”  sum=3

Output - Count of numbers smaller than or equal to N with given digit sum are: 10

Explanation - The numbers upto 1000 that have sum of digits equal to 3 are :-

3, 12, 21, 30, 102, 111, 120, 201, 210 and 300.

## Approach used in the below program is as follows

In this approach we will use dynamic programming to store the sums of numbers and their digits in array arr. Here 18 is for maximum 18 digit numbers, 2 is for values 0 and 1. And 162 is for maximum sum possible if all 18 digits are 9 ( 18*9=162 ).

Value of element arr[i][j][k] represents count of numbers whose first i digits are taken into consideration and j is 0 or 1 based on whether the i digit current constructed number is less or more than first i digit number in N. ( if N is 123 and i is 2 then we will check if 2 digit numbers are equal or more than 12. If equal to 12 then j will be 1 else j will be 0 in arr[i][j][k] ). The index k will be the sum of i digits in arr[i][j][k].

If i=digits of N and k=input sum then result will be 1 else 0.

For filling the next digit ( i+1th ) we will check j. If j is 1 then i-1 digits are equal to i-1 digits of N so the next digit can only have values between 0 to i+1 th digit of N to make it <= N.

If j is 0 then the i-1 digit number is less than i-1 digit number in N. So we can place any value between 0 to 9 for the next digit ( i+1 th ) position and it will still be less than N.

At the end return the result as a final count.

• Take string str representing a number N and total as sum of digits.
• Take array arr and initialize it with -1 using memset.
• Function count_digits(int i, bool check, int temp, int total, string str, int size) recursively fills arr[][][] and at the end returns count of numbers smaller than or equal to N with a given digit sum.
• If the current number of digits i is equal to digits of N and current sum temp is equal to input sum total then return 1 else return 0.
• Take count = arr[i][check][temp]. If it is not -1 then return count as result.
• Take temporary variables check_2 ( bool ) and temp_2. (int ).
• Traverse digits 0 to 9 using for loop and compare if check is 1 or 0. If 1 then compare current digit ch with str[i]. If it is greater then break the loop ( do nothing ).
• Set check_2 = check || ch < str[i].
• Set temp_2 = temp + (ch - '0') as current sum.
• Add count_digits(i + 1, check_2, temp_2, total, str, size) to count for recursively calculating sum of digits.
• At the end return count as result.

## Example

#include <bits/stdc++.h>
using namespace std;
int arr;
int count_digits(int i, bool check, int temp, int total, ing str, int size) {
if (i == size) {
if (temp == total) {
return 1;
} else {
return 0;
}
}
int count = arr[i][check][temp];
if (count != -1) {
return count;
}
count = 0;
bool check_2;
int temp_2;
for (char ch = '0'; ch <= '9'; ch++) {
if (!check) {
if (ch > str[i]) {
break;
}
}
check_2 = check || ch < str[i];
temp_2 = temp + (ch - '0');
count += count_digits(i + 1, check_2, temp_2, total, str, size);
}
return count;
}
int main() {
string str = "1101";
int size = str.size();
int total = 5;
memset(arr, -1, sizeof(arr));
cout << "Count of numbers smaller than or equal to N with given digit sum are: " << count_digits(0, 0, 0, total, str, size);
return 0;
}

If we run the above code it will generate the following output −

## Output

Count of numbers smaller than or equal to N with given digit sum are: 26