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 = 2Output:
10Explanation:
Example 2:
Input:
Output:
Explanation:
Since $r > n$, $C(2,4)=0$.
Example 3:
Input:
Output:
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 MODfor $0 \le i \le n$.Compute
inv[n] = fact[n]^(MOD-2) mod MODvia fast exponentiation.Build
inv[i-1] = inv[i] * i mod MODfor $i = n$ down to 1 (inverse factorials).Return
C(n,r)=fact[n]×inv[r]×inv[n−r] mod MOD.
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:
Compute inverse factorial of n:
using binary exponentiation in $O(\log,\text{MOD})$.
Compute remaining inverses:
Combine:
🧮 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++)
⚡ Alternative Approaches
📊 2️⃣ DP Table (Pascal’s Triangle)
Idea
Use the identity $C(i, j) = C(i-1, j-1) + C(i-1, j)$ to build a DP table of size $(n+1)\times(r+1)$.
Algorithm Steps
Allocate
C[n+1][r+1], initialize with zeros.For each
iin0..n:For each
jin0..min(i,r):If
j == 0orj == i:C[i][j]=1.Else:
C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD.
Return
C[n][r].
⏱️ Time: $O(n \times r)$ 🗂️ Space: $O(n \times r)$
📊 3️⃣ Recursive + Memoization
Idea
Use recursion on the identity $C(n,r)=C(n-1,r-1)+C(n-1,r)$ with memoization to avoid recomputation.
Algorithm Steps
Base: if
r == 0orr == n, return 1.If
dp[n][r]is cached, return it.Otherwise compute
(solve(n-1,r-1) + solve(n-1,r)) % MOD, store indp[n][r].
⏱️ Time: $O(n \times r)$ 🗂️ Space: $O(n \times r)$
🆚 Comparison of Approaches
Approach
⏱️ Time
🗂️ Space
✅ Pros
⚠️ Cons
Modular Inverse + Factorials
🟢 $O(n + \log,\text{MOD})$
🟢 $O(n)$
Fast; handles large $n$; reusable
Requires mod exponentiation step
DP Table
🔸 $O(n \times r)$
🔸 $O(n \times r)$
Simple; no exponentiation
Higher time/memory for large inputs
Recursive + Memoization
🔸 $O(n \times r)$
🔸 $O(n \times r)$
Elegant recursion
Recursion/memo overhead; risk of stack
✅ Best Choice?
Use Case
Recommended Approach
Single query or multiple large queries
🥇 Modular Inverse + Factorials
Educational/demo or small $n,r$
🥈 DP Table
Academic demonstration of recursion
🥉 Recursive + Memoization
🧑💻 Code (Java)
🐍 Code (Python)
🧠 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