nCr | Practice | GeeksforGeeks

Given two integers n and r, find nCr. Since the answer may be very large, calculate the answer modulo 109+7.
Example 1:
Input: n = 3, r = 2
Output: 3
Explaination: 3C2 = 3. 

Example 2:
Input: n = 2, r = 4
Output: 0
Explaination: r is greater than n.

Your Task:
You do not need to take input or print anything. Your task is to complete the function nCr() which takes n and r as input parameters and returns nCr modulo 109+7..

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

Constraints:
1 ≤ n ≤ 1000
1 ≤ r ≤ 800
0
aryabamboli200210 hours ago
Easy Pascal's Triangle based Solution
int size = min(r,n-r);
        vector<int> dp(size+1,0);
        dp[0]=1;
        for(int i=1;i<=n;i++){
            for(int j=size;j>=1;j--){
                dp[j]=(dp[j]+dp[j-1])%mod;
            }
        }
        return dp[size];
0
f2020155610 hours ago
int size = min(r,n-r);
        vector<int> dp(size+1,0);
        dp[0]=1;
        for(int i=1;i<=n;i++){
            for(int j=size;j>=1;j--){
                dp[j]=(dp[j]+dp[j-1])%mod;
            }
        }
        return dp[size];
+1
adarsh444 days ago
Method -1:
int nCr(int n, int r){
        const int mod = 1e9 + 7; 
        int dp[n+1][r+1]; 
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++){
            for(int j = 0; j <= i and j <= r; j++){
                if (j == 0 or i == j)
                    dp[i][j] = 1;  
                else 
                    dp[i][j] = (dp[i-1][j-1] + dp[i-1][j]) % mod; 
            }
        }
        return dp[n][r]; 
    }
Method -2:
    // Space Optimised. 
    int nCr(int n , int r){
        // Using pascal's traingle 
        const int mod = 1e9 + 7; 
        int dp[r+1]; 
        memset(dp, 0, sizeof(dp)); 
        dp[0] = 1 ; 
        for(int i = 1; i <= n; i++){
            for(int j = min(i, r); j >= 1; j--)
                dp[j] = (dp[j] + dp[j-1]) % mod; 
        }
        return dp[r]; 
    }
 
0
deepak202040584 days ago
   int nCr(int n, int r){   int dp[n+1];   dp[0]=1;   for(int i=1;i<n;i++){       dp[i]=dp[i-1]*i;   }   int m=(int) dp[n]/(dp[r]*dp[n-r]);   return m%1000000007;   }
 
isme kya probelem hai?
 
+3
vmsreeram021 week ago
Direct formula recursion
Use the formula
        nCr = (n-1) C (r-1) + (n-1) C (r)
long long dp[1005][805];
long long ncr(int n, int r)
{
    if(n<r)return 0;
    if(n==r)return 1;
    if(r==0)return 1;
    if(r==1)return n;
    if(dp[n][r]!=-1) return dp[n][r];
    return dp[n][r]=(ncr(n-1,r-1)+ncr(n-1,r))%int(1e9+7);
}
int nCr(int n, int r){
    // code here
    for(int i=0;i<=n+1;i++)for(int j=0;j<=r+1;j++)dp[i][j]=-1;
    return ncr(n,r);
}
0
codehackss1 week ago
No DP SOLution - Python 10 lines code
class Solution:
    def nCr(self, n, r):
        if r > n:
            return 0
        ans = 1  
        maxx = max(r,n-r)
        for i in range(n,maxx,-1):
            ans*=i
        minn = min(r,n-r)
        factt = 1
        for j in range(2,minn+1):
            factt*=j
        return (ans//factt)%(pow(10,9) + 7)
+1
2019uee01191 week ago
//using the concept of pascal's traingle 
//Simple code if you know pascal traingle
vector<int> nthRowOfPascalTriangle(int n)   {       vector<vector<int>>v(n);       int mod =1000000007;       for(int i=0;i<n;i++)       {           v[i].resize(i+1);           v[i][0]=v[i][i]=1;           for(int j=1;j<i;j++)           {               v[i][j]=((v[i-1][j]%mod+v[i-1][j-1]%mod)%mod)%mod;           }       }       return v[n-1];   }
   int nCr(int n, int r)   {       if(r>n)       {           return 0;       }       if(r==n)       {           return 1;       }       vector<int>ans;       ans=nthRowOfPascalTriangle(n+1);       return ans[r];   }
0
akkeshri140420012 weeks ago
const int mod=1e9+7;
   int nCr(int n, int r){
       // code here
       if(n<r){
           return 0;
       }
       vector<vector<int>>dp(n+1,vector<int>(r+1));
       for(int i=0;i<n+1;i++){
           dp[i][0]=1;
       }
       for(int j=0;j<r+1;j++){
           dp[0][j]=0;
       }
       dp[0][0]=1;
   
       for(int i=1;i<n+1;i++){
           for(int j=1;j<r+1;j++){
               if(j>i){
                   dp[i][j]=0;
               }
               else{
               dp[i][j]=((dp[i-1][j])%mod + (dp[i-1][j-1])%mod)%mod;
               }
               
           }
       }
       return dp[n][r];
       
   }
+1
somplz01092 weeks ago
SIMPLE C++ RECURSIVE CODE : 
class Solution{
public:

    int dp[1001][801];
    
    int nccr(int n,int r){
        if(r > n)return 0;
        if(r == n || r==0)return 1;
        if(dp[n][r]!=-1)return dp[n][r];
        return dp[n][r] = (nccr(n-1,r) + nccr(n-1,r-1))%1000000007;
    }
    
    int nCr(int n, int r){
        // code here
        memset(dp,-1,sizeof(dp));
        return nccr(n,r);
    }
};
0
rk2207rajesh2 weeks ago
public:   int nCr(int n, int R){              vector<vector<ll>>dp(n+1,vector<ll>(R+1,0));              for(int i=0;i<=n;i++)       {           for(int r=0;r<=R;r++)           {               if(r==0) dp[i][r]=1;               else if(i==0)  dp[i][r]=0;               else               {                   ll exclude=dp[i-1][r]%mod;                   ll include=0;                   if(r<=i)                      include=dp[i-1][r-1]%mod;                 dp[i][r]= (exclude+include)%mod;               }           }       }       return dp[n][R];   }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.