Count of n digit numbers whose sum of digits equals to given sum | Practice | GeeksforGeeks

Given two integers ‘n’ and ‘sum’, find the count of all n digit numbers whose sum of digits is ‘sum’. Leading 0’s are not counted as digits. 

Example 1:
Input: n = 2, sum = 2
Output: 2
Explaination: 
The 2 digit numbers are 11 and 20.

Example 2:
Input: n = 1, sum = 10
Output: -1
Explaination: 
We cannot get sum as 10 from a single digit.

Your Task:
You do not need to read input or print anything. Your task is to complete the function countWays() which takes the value n and sum as input parameters and returns the number of possible ways modulo 109+7. If there is no possible way then it returns -1.

Expected Time Complexity: O(n*sum)
Expected Auxiliary Space: O(n*sum)

Constraints:
1 ≤ n ≤ 102
1 ≤ sum ≤ 103
0
imranwahid6 days ago
Easy C++ solution using memoization
0
ankitpodder3 months ago
Total Time Taken:
0.2/1.2
 
long int countWays(int N, int Sum){
        lli ans = 0;
        vvlli dp(N+1 , vector<lli>(Sum+1 , 0));
        dp[0][0] = 1;
        for(int n = 1 ; n <= N ; n++){
            for(int s = 1 ; s <= Sum ; s++){
                for(int d = 0 ; d < 
                	min(10 , s+1) ; d++
                ){
                    if(d == 0 && n == 1) continue;
                    lli r = (dp[n-1][s-d] % di);
                    dp[n][s] += r;
                    dp[n][s] %= di;
                }
            }
        }
        return dp[N][Sum]==0 ? -1 : dp[N][Sum];
    }
0
VickySam19019 months ago
VickySam1901
class Solution{public:    long int count(int n,int sum,vector<vector<int>> &vec){        if(n==0){            return sum==0;        }        if(vec[n][sum]!=-1){            return vec[n][sum];        }        long int ans=0;        for(int i=0;i<=9;i++){            if(sum-i>=0){                ans+=count(n-1,sum-i,vec);                ans=ans%1000000007;            }        }        return vec[n][sum]=ans;    }    long int countWays(int n, int Sum){        vector<vector<int>> vec(n+1,vector<int>(Sum+1,-1));        long int ans=0;        for(int i=1;i<=9;i++){            if(Sum-i>=0){                ans+=count(n-1,Sum-i,vec);                ans=ans%1000000007;            }        }        return ans?ans:-1;    }};
0
VickySam1901
This comment was deleted.
0
Debjyoti Chattopadhyay10 months ago
Debjyoti Chattopadhyay
class Solution{    static long countWays(int n, int Sum)    {        long store[][]=new long[n+1][Sum+1];        for(int i=0;i<=n;i++){            for(int j=0;j<=Sum;j++)                store[i][j]=-1;        }        long val=dp(n,Sum,store);        if(val==0)            return(-1);        return(val);    }//countWays    public static long dp(int i,int sum,long store[][]){        if(sum<=0 && i!=0)            return(0);        if(i==1){            if(sum<10)                return(1);            return(0);        }        if(store[i][sum]!=-1)            return(store[i][sum]);        long ans=0;        for(int j=0;j<=9;j++){            long v=dp(i-1,sum-j,store);            ans=(ans+v)%1000000007;        }        store[i][sum]=ans%1000000007;        return(ans);    }//dp}//SolutionJAVA soln
0
HUNTER10 months ago
HUNTER
long int countWays(int n, int Sum){        // code here       if(n==1&&Sum<10)       return 1;        long int dp[n+1][Sum+1];       memset(dp,0,sizeof dp);       dp[0][0]=1;        for(int i=1;i<=n;i++)       for(int j=1;j<=Sum;j++)       for(int k=0;k<10&&k<=j;k++)       dp[i][j]=(dp[i][j]+dp[i-1][j-k])%1000000007;        return dp[n][Sum]?dp[n][Sum]:-1;     }
0
Jdragon jds2 years ago
Jdragon jds
dude n*sum complexity thats the first thing came to my mind ,without dp
0
Archit2 years ago
Archit
Python Simple DP Solutionhttps://ide.geeksforgeeks.o...
0
Akshay Solanki2 years ago
Akshay Solanki
JAVA simple DP solution.https://ide.geeksforgeeks.o...
0
Sachin Negi2 years ago
Sachin Negi
python memoization  https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.