Space and time efficient Binomial Coefficient
Write a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2.
We have discussed a O(n*k) time and O(k) extra space algorithm in this post. The value of C(n, k) can be calculated in O(k) time and O(1) extra space.
C(n, k) = n! / (n-k)! * k!
= [n * (n-1) *....* 1] / [ ( (n-k) * (n-k-1) * .... * 1) *
( k * (k-1) * .... * 1 ) ]
After simplifying, we get
C(n, k) = [n * (n-1) * .... * (n-k+1)] / [k * (k-1) * .... * 1]
Also, C(n, k) = C(n, n-k) // we can change r to n-r if r > n-r
Following implementation uses above formula to calculate C(n, k)
C++
// Program to calculate C(n ,k) #include <bits/stdc++.h> using namespace std; // Returns value of Binomial Coefficient C(n, k) int binomialCoeff(int n, int k) { int res = 1; // Since C(n, k) = C(n, n-k) if ( k > n - k ) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Driver Code int main() { int n = 8, k = 2; cout << "Value of C(" << n << ", " << k << ") is " << binomialCoeff(n, k); return 0; } // This is code is contributed by rathbhupendra |
chevron_right
filter_none
C
// Program to calculate C(n ,k) #include <stdio.h> // Returns value of Binomial Coefficient C(n, k) int binomialCoeff(int n, int k) { int res = 1; // Since C(n, k) = C(n, n-k) if ( k > n - k ) k = n - k; // Calculate value of [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } /* Driver program to test above function*/int main() { int n = 8, k = 2; printf ("Value of C(%d, %d) is %d ", n, k, binomialCoeff(n, k) ); return 0; } |
chevron_right
filter_none
Java
// Program to calculate C(n ,k) in java class BinomialCoefficient { // Returns value of Binomial Coefficient C(n, k) static int binomialCoeff(int n, int k) { int res = 1; // Since C(n, k) = C(n, n-k) if ( k > n - k ) k = n - k; // Calculate value of [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } /* Driver program to test above function*/ public static void main(String[] args) { int n = 8; int k = 2; System.out.println("Value of C("+ n + ", " + k+ ") " + "is" + " "+ binomialCoeff(n, k)); } } // This Code is Contributed by Saket Kumar |
chevron_right
filter_none
Python
# Python program to calculate C(n ,k) # Returns value of Binomial Coefficient # C(n, k) def binomialCoefficient(n, k): # since C(n, k) = C(n, n - k) if(k > n - k): k = n - k # initialize result res = 1 # Calculate value of # [n * (n-1) *---* (n-k + 1)] / [k * (k-1) *----* 1] for i in range(k): res = res * (n - i) res = res / (i + 1) return res # Driver program to test above function n = 8k = 2res = binomialCoefficient(n, k) print("Value of C(%d, %d) is %d" %(n, k, res)) # This code is contributed by Aditi Sharma |
chevron_right
filter_none
C#
// C# Program to calculate C(n, k) using System; class BinomialCoefficient { // Returns value of Binomial // Coefficient C(n, k) static int binomialCoeff(int n, int k) { int res = 1; // Since C(n, k) = C(n, n-k) if ( k > n - k ) k = n - k; // Calculate value of [n * ( n - 1) *---* ( // n - k + 1)] / [k * (k - 1) *----* 1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // Driver Code public static void Main() { int n = 8; int k = 2; Console.Write("Value of C("+ n + ", " + k+ ") " + "is" + " "+ binomialCoeff(n, k)); } } // This Code is Contributed by // Smitha Dinesh Semwal. |
chevron_right
filter_none
PHP
<?php // Program to calculate C(n ,k) // Returns value of Binomial // Coefficient C(n, k) function binomialCoeff($n,$k) { $res = 1; // Since C(n, k) = C(n, n-k) if ( $k > $n - $k ) $k = $n - $k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / // [k * (k-1) *----* 1] for ($i = 0; $i < $k; ++$i) { $res *= ($n - $i); $res /= ($i + 1); } return $res; } // Driver Code $n = 8; $k = 2; echo " Value of C ($n, $k) is ", binomialCoeff($n, $k); // This code is contributed by ajit. ?> |
chevron_right
filter_none
Value of C(8, 2) is 28
Time Complexity: O(k)
Auxiliary Space: O(1)
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Binomial Coefficient | DP-9
- Sum of product of r and rth Binomial Coefficient (r * nCr)
- Maximum binomial coefficient term value
- Eggs dropping puzzle (Binomial Coefficient and Binary Search Solution)
- Space efficient iterative method to Fibonacci number
- Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space
- Merge Sort with O(1) extra space merge and O(n lg n) time
- Permutation Coefficient
- Clustering Coefficient in Graph Theory
- Program to find correlation coefficient
- Replace the maximum element in the array by coefficient of range
- Sum of Binomial coefficients
- Corollaries of Binomial Theorem

