
Problem
Solution
Submissions
Compute the Binomial Coefficient
Certification: Intermediate Level
Accuracy: 50.94%
Submissions: 53
Points: 10
Write a Python function to compute the binomial coefficient C(n, k), which is the number of ways to choose k elements from a set of n elements.
Example 1
- Input: n = 5, k = 2
- Output: 10
- Explanation:
- Step 1: Calculate C(5,2) which gives the number of ways to choose 2 elements from a set of 5.
- Step 2: Using the formula C(n,k) = n! / (k! * (n-k)!), we have C(5,2) = 5! / (2! * 3!).
- Step 3: This equals 5! / (2! * 3!) = 120 / (2 * 6) = 120 / 12 = 10.
- Step 4: So there are 10 ways to choose 2 elements from a set of 5 elements.
Example 2
- Input: n = 7, k = 3
- Output: 35
- Explanation:
- Step 1: Calculate C(7,3) which gives the number of ways to choose 3 elements from a set of 7.
- Step 2: Using the formula C(n,k) = n! / (k! * (n-k)!), we have C(7,3) = 7! / (3! * 4!).
- Step 3: This equals 7! / (3! * 4!) = 5040 / (6 * 24) = 5040 / 144 = 35.
- Step 4: So there are 35 ways to choose 3 elements from a set of 7 elements.
Constraints
- 0 <= k <= n <= 50
- Time Complexity: O(n * k)
- Space Complexity: O(n * k)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use dynamic programming to compute the binomial coefficient.
- Initialize a 2D array to store intermediate results.
- Use the formula C(n, k) = C(n-1, k-1) + C(n-1, k).