13. nCr
The problem can be found at the following link: 🔗 Question Link
🧩 Problem Description
Given two integers n
and r
, return the value of the binomial coefficient C(n, r), defined as the number of ways to choose r
objects from a set of n
without regard to order. It is given by:
If $r > n$, return 0. It is guaranteed that the result will fit within a 32-bit integer.
📘 Examples
Example 1:
Input:
n = 5, r = 2
Output:
10
Explanation:
Example 2:
Input:
n = 2, r = 4
Output:
0
Explanation:
Since $r > n$, $C(2,4)=0$.
Example 3:
Input:
n = 5, r = 0
Output:
1
Explanation:
🔒 Constraints
$1 \leq n \leq 100$
$0 \leq r \leq 100$
✅ My Approach
Modular Inverse + Factorials
We compute factorials up to n
modulo $10^9+7$, then use Fermat’s little theorem to find modular inverses in $O(n + \log(\text{MOD}))$:
Precompute
fact[i] = i! mod MOD
for $0 \le i \le n$.Compute
inv[n] = fact[n]^(MOD-2) mod MOD
via fast exponentiation.Build
inv[i-1] = inv[i] * i mod MOD
for $i = n$ down to 1 (inverse factorials).Return
Algorithm Steps:
Handle if $r > n$: return 0.
Initialize constants:
MOD = 10^9+7
.Allocate two arrays of size
n+1
:fact[]
,inv[]
.Compute factorials:
fact[0] = 1 for i in 1..n: fact[i] = fact[i-1] * i % MOD
Compute inverse factorial of n:
inv[n] = pow_mod(fact[n], MOD-2)
using binary exponentiation in $O(\log,\text{MOD})$.
Compute remaining inverses:
for i = n..1: inv[i-1] = inv[i] * i % MOD
Combine:
return fact[n] * inv[r] % MOD * inv[n-r] % MOD
🧮 Time and Auxiliary Space Complexity
Expected Time Complexity: $O(n + \log(\text{MOD}))$, since we build factorials in $O(n)$, compute one modular exponentiation in $O(\log,\text{MOD})$, and build inverse factorials in $O(n)$.
Expected Auxiliary Space Complexity: $O(n)$, for storing
fact[]
andinv[]
arrays of size $n+1$.
🧠 Code (C++)
class Solution {
static const int MOD = 1e9+7;
long long modexp(long long x, long long y) {
long long res = 1;
while (y) {
if (y & 1) res = res * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return res;
}
public:
int nCr(int n, int r) {
if (r > n) return 0;
vector<long long> fact(n+1, 1), inv(n+1);
for (int i = 1; i <= n; ++i)
fact[i] = fact[i-1] * i % MOD;
inv[n] = modexp(fact[n], MOD-2);
for (int i = n; i > 0; --i)
inv[i-1] = inv[i] * i % MOD;
return fact[n] * inv[r] % MOD * inv[n-r] % MOD;
}
};
🧑💻 Code (Java)
class Solution {
static final int MOD = (int)1e9+7;
long modexp(long x, long y) {
long res = 1;
while (y > 0) {
if ((y & 1) == 1) res = res * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return res;
}
public int nCr(int n, int r) {
if (r > n) return 0;
long[] fact = new long[n+1], inv = new long[n+1];
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i-1] * i % MOD;
inv[n] = modexp(fact[n], MOD-2);
for (int i = n; i > 0; --i)
inv[i-1] = inv[i] * i % MOD;
return (int)(fact[n] * inv[r] % MOD * inv[n-r] % MOD);
}
}
🐍 Code (Python)
class Solution:
MOD = 10**9 + 7
def power(self, x, y):
res = 1
while y:
if y & 1:
res = res * x % self.MOD
x = x * x % self.MOD
y >>= 1
return res
def nCr(self, n: int, r: int) -> int:
if r > n:
return 0
fact = [1] * (n+1)
for i in range(1, n+1):
fact[i] = fact[i-1] * i % self.MOD
inv = [1] * (n+1)
inv[n] = self.power(fact[n], self.MOD-2)
for i in range(n, 0, -1):
inv[i-1] = inv[i] * i % self.MOD
return fact[n] * inv[r] % self.MOD * inv[n-r] % self.MOD
🧠 Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions?. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated