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
bijeshkumarch15 hours ago
#in just 0.3
#store the values of fact_r only and use it for others
 
if r>n:           return 0       if r==0:           return 1       arr=[0]*r       tmp=1       fact_r=1       while tmp<=r:           fact_r*=tmp           arr[tmp-1]=fact_r           tmp+=1       n_r=n-r       fact_n_r=1       if n_r<=r:           fact_n_r=arr[n_r-1]       else:           fact_n_r=fact_r           tmp1=r+1           while tmp1<=n_r:               fact_n_r*=tmp1               tmp1+=1       fact_n=fact_r       tmp1=r+1       while tmp1<=n:           fact_n*=tmp1           tmp1+=1       res=(fact_n//(fact_n_r*fact_r))%1000000007       return res
0
18pa1a047716 hours ago
top down
    def ncr(self,n,r,dp):       if r>n:           return 0       if r==n or r==0:           return 1       if dp[n][r]!=-1:           return dp[n][r]       dp[n][r]=self.ncr(n-1,r-1,dp)%(10**9+7)+self.ncr(n-1,r,dp)%(10**9+7)       return dp[n][r]%(10**9+7)
 
 
0
moaslam8262 days ago
c++ easy soln
const int M=1e9+7;
int fun(int n,int r,vector<vector<int>> &dp){
	if(dp[n][r]!=-1) return dp[n][r];
	if(n==0) return dp[n][r]=0;
	if(r==0) return dp[n][r]=1;
	if(r==n) return dp[n][r]=1;
	int a=fun(n-1,r-1,dp)%M;
	int b=fun(n-1,r,dp)%M;
	int ans=a+b;
	ans%=M;
	return dp[n][r]=ans;
}
    int nCr(int n, int r){
      vector<vector<int>> dp(n+1,vector<int>(r+1,-1));
	     return fun(n,r,dp);
    }
0
chandankasamsetty4 days ago
class Solution{
int p = 1e9+7;
public:
    int helper(int n,int r,vector<vector<int>>&dp)
    {
        if(r==0|r==n)
            return 1;
        if(dp[n][r]!=-1)
            return dp[n][r];
        else
            return dp[n][r] = (helper(n-1,r-1,dp)%p+helper(n-1,r,dp)%p)%p;
    }
    int nCr(int n, int r){
    vector<vector<int>>dp(n+1,vector<int>(r+1,-1));
    if(n<r)
        return 0;
    else
        return helper(n,r,dp);
    }
};
+1
sdmrf3 weeks ago
Python Soln:
from math import factorial as F
class Solution:
    def nCr(self, n, r):
        return (F(n)//(F(r)*F(abs(n-r))))%(10**9+7)
+1
mailtoamandeepsingh281 month ago
C++ code
    int nCr(int n, int r){       if(r>n)           return 0;       if(r==n)           return 1;       if((n-r)<r)           r=n-r;              int mod = 1000000007;              vector<int>dp(r+1, 0);       dp[0]=1;              for(int i=1;i<=n;i++){           for(int j=min(r, i);j>=1;j--){               dp[j] = (dp[j] + dp[j-1])%mod;           }       }              return dp[r];   }
0
ankitghanghas10721 month ago
int inf = 1e9 + 7;
int nCr(int n, int r){       // code here       if(r > n)           return 0;               if(r == n || r == 0)           return 1;       if(r == 1)           return n;       if(r > (n-r))           return nCr(n,n-r);              long long dp[n+1][r+1];       for(int j = 1; j<=n; j++)           dp[j][1] = j;                  for(int i = 2; i<=n; i++){           for(int j = 2; j<=r && j<=i; j++){               if(i == j)                   dp[i][j] = 1;               else dp[i][j] = (dp[i-1][j-1] + dp[i-1][j])%inf;                          }       }              return dp[n][r];          }
0
annanyamathur1 month ago
int approx=1e9+7;   int nCr(int n, int r){       vector<int>memo(r+1,0);       memo[0]=1;       for(int i=1;i<=n;i++)       {           for(int j=r;j>0;j--)           {               memo[j]=(memo[j-1]%approx+memo[j]%approx)%approx;           }       }       return memo[r];   }
-1
swapniltayal4222 months ago
class Solution:   def nCr(self, n, r):       # code here       if r > n:           return 0       def fact(n):           p = 1           for i in range(1, n+1):               p = p * i           return p       return (fact(n)//(fact(r)*fact(n-r)))%1000000007
 
0
swapniltayal4222 months ago
class Solution:   def nCr(self, n, r):       # code here              if r > n:           return 0                  def fact(n):           p = 1           for i in range(1, n+1):               p = p * i           return p              a = fact(n)       b = fact(r)       c = fact(n-r)              d = b*c              s = (a//d)%1000000007              return s
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.