Given two integers ‘n’ and ‘sum’, find count of all n digit numbers with sum of digits as ‘sum’. Leading 0’s are not counted as digits.
1 <= n <= 100 and 1 <= sum <= 50000

Example:

Input:  n = 2, sum = 2
Output: 2
Explanation: Numbers are 11 and 20

Input:  n = 2, sum = 5
Output: 5
Explanation: Numbers are 14, 23, 32, 41 and 50

Input:  n = 3, sum = 6
Output: 21

The idea is simple, we subtract all values from 0 to 9 from given sum and recur for sum minus that digit. Below is recursive formula.

    countRec(n, sum) = ∑countRec(n-1, sum-x)
                            where 0 =< x <= 9 and
                                 sum-x >= 0

    One important observation is, leading 0's must be
    handled explicitly as they are not counted as digits.
    So our final count can be written as below.
    finalCount(n, sum) = ∑countRec(n-1, sum-x)
                           where 1 =< x <= 9 and
                                 sum-x >= 0
[ad type=”banner”]

Below is a simple recursive solution based on above recursive formula.

[pastacode lang=”cpp” manual=”%2F%2F%20A%20recursive%20program%20to%20count%20numbers%20with%20sum%0A%2F%2F%20of%20digits%20as%20given%20’sum’%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Recursive%20function%20to%20count%20’n’%20digit%20numbers%0A%2F%2F%20with%20sum%20of%20digits%20as%20’sum’.%20This%20function%0A%2F%2F%20considers%20leading%200’s%20also%20as%20digits%2C%20that%20is%0A%2F%2F%20why%20not%20directly%20called%0Aunsigned%20long%20long%20int%20countRec(int%20n%2C%20int%20sum)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20case%0A%20%20%20%20if%20(n%20%3D%3D%200)%0A%20%20%20%20%20%20%20return%20sum%20%3D%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20answer%0A%20%20%20%20unsigned%20long%20long%20int%20ans%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Traverse%20through%20every%20digit%20and%20count%0A%20%20%20%20%2F%2F%20numbers%20beginning%20with%20it%20using%20recursion%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3C%3D9%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20if%20(sum-i%20%3E%3D%200)%0A%20%20%20%20%20%20%20%20%20%20ans%20%2B%3D%20countRec(n-1%2C%20sum-i)%3B%0A%20%0A%20%20%20%20return%20ans%3B%0A%7D%0A%20%0A%2F%2F%20This%20is%20mainly%20a%20wrapper%20over%20countRec.%20It%0A%2F%2F%20explicitly%20handles%20leading%20digit%20and%20calls%0A%2F%2F%20countRec()%20for%20remaining%20digits.%0Aunsigned%20long%20long%20int%20finalCount(int%20n%2C%20int%20sum)%0A%7B%0A%20%20%20%20%2F%2F%20Initialize%20final%20answer%0A%20%20%20%20unsigned%20long%20long%20int%20ans%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Traverse%20through%20every%20digit%20from%201%20to%0A%20%20%20%20%2F%2F%209%20and%20count%20numbers%20beginning%20with%20it%0A%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%3D%209%3B%20i%2B%2B)%0A%20%20%20%20%20%20if%20(sum-i%20%3E%3D%200)%0A%20%20%20%20%20%20%20%20%20ans%20%2B%3D%20countRec(n-1%2C%20sum-i)%3B%0A%20%0A%20%20%20%20return%20ans%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%202%2C%20sum%20%3D%205%3B%0A%20%20%20%20cout%20%3C%3C%20finalCount(n%2C%20sum)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++” highlight=”” provider=”manual”/]

Output:

5

The time complexity of above solution is exponential. If we draw the complete recursion tree, we can observer that many subproblems are solved again and again. For example, if we start with n = 3 and sum = 10, we can reach n = 1, sum = 8, by considering digit sequences 1,1 or 2, 0.
Since same suproblems are called again, this problem has Overlapping Subprolems property. So min square sum problem has both properties of a dynamic programming problem.

Below is Memoization based the implementation.

[pastacode lang=”cpp” manual=”%2F%2F%20A%20memoization%20based%20recursive%20program%20to%20count%20%0A%2F%2F%20numbers%20with%20sum%20of%20n%20as%20given%20’sum’%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20lookup%20table%20used%20for%20memoization%0Aunsigned%20long%20long%20int%20lookup%5B101%5D%5B50001%5D%3B%0A%20%0A%2F%2F%20Memoizatiob%20based%20implementation%20of%20recursive%0A%2F%2F%20function%0Aunsigned%20long%20long%20int%20countRec(int%20n%2C%20int%20sum)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20case%0A%20%20%20%20if%20(n%20%3D%3D%200)%0A%20%20%20%20%20%20%20return%20sum%20%3D%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20this%20subproblem%20is%20already%20evaluated%2C%0A%20%20%20%20%2F%2F%20return%20the%20evaluated%20value%0A%20%20%20%20if%20(lookup%5Bn%5D%5Bsum%5D%20!%3D%20-1)%0A%20%20%20%20%20%20%20return%20lookup%5Bn%5D%5Bsum%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20answer%0A%20%20%20%20unsigned%20long%20long%20int%20ans%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Traverse%20through%20every%20digit%20and%0A%20%20%20%20%2F%2F%20recursively%20count%20numbers%20beginning%0A%20%20%20%20%2F%2F%20with%20it%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3C10%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20if%20(sum-i%20%3E%3D%200)%0A%20%20%20%20%20%20%20%20%20%20ans%20%2B%3D%20countRec(n-1%2C%20sum-i)%3B%0A%20%0A%20%20%20%20return%20lookup%5Bn%5D%5Bsum%5D%20%3D%20ans%3B%0A%7D%0A%20%0A%2F%2F%20This%20is%20mainly%20a%20wrapper%20over%20countRec.%20It%0A%2F%2F%20explicitly%20handles%20leading%20digit%20and%20calls%0A%2F%2F%20countRec()%20for%20remaining%20n.%0Aunsigned%20long%20long%20int%20finalCount(int%20n%2C%20int%20sum)%0A%7B%0A%20%20%20%20%2F%2F%20Initialize%20all%20entries%20of%20lookup%20table%0A%20%20%20%20memset(lookup%2C%20-1%2C%20sizeof%20lookup)%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20final%20answer%0A%20%20%20%20unsigned%20long%20long%20int%20ans%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Traverse%20through%20every%20digit%20from%201%20to%0A%20%20%20%20%2F%2F%209%20and%20count%20numbers%20beginning%20with%20it%0A%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%3D%209%3B%20i%2B%2B)%0A%20%20%20%20%20%20if%20(sum-i%20%3E%3D%200)%0A%20%20%20%20%20%20%20%20%20ans%20%2B%3D%20countRec(n-1%2C%20sum-i)%3B%0A%20%20%20%20return%20ans%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%203%2C%20sum%20%3D%205%3B%0A%20%20%20%20cout%20%3C%3C%20finalCount(n%2C%20sum)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

5