Open In App

Binomial Coefficient

Last Updated : 17 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given an integer values n and k, the task is to find the value of Binomial Coefficient C(n, k).

  • A binomial coefficient C(n, k) can be defined as the coefficient of x^k in the expansion of (1 + x)^n.
  • A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects more formally, the number of k-element subsets (or k-combinations) of a n-element set.

Examples

Input: n = 4, k = 2
Output: 6
Explanation: The value of 4C2 is (4 × 3) / (2 × 1) = 6.

Input: n = 5, k = 2
Output: 10
Explanation: The value of 5C2 is (5 × 4) / (2 × 1) = 10.

Input: n = 6, k = 3
Output: 20
Explanation: The value of 6C3 is (6 × 5 × 4) / (3 × 2 × 1) = 20.

Using recursion – O(2 ^ n)  Time and O(n) Space

The idea is to use recursion to find C(n, k).The value of C(n, k) can be recursively calculated using the following standard formula for Binomial Coefficients. C(n, k) = C(n-1, k-1) + C(n-1, k). C(n, 0) = C(n, n) = 1. So we just need to make recursive calls of C(n-1, k-1) and C(n – 1, k). The base conditions will be when k = 0 or value of k and n be equal.

C++
// C++ implementation to find 
// Binomial Coefficient using recursion

#include <bits/stdc++.h>
using namespace std;

// Returns value of Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k) {
  	
    // k can not be grater then k so we return 0 here
    if (k > n)
        return 0;
  
  	// base condition when k and n are equal or k = 0
    if (k == 0 || k == n)
        return 1;

    // Recurvie add the value 
    return binomialCoeff(n - 1, k - 1)
           + binomialCoeff(n - 1, k);
}

int main() {
    int n = 5, k = 2;
    cout << binomialCoeff(n, k);
    return 0;
}
C Java Python C# JavaScript

Output
10

Top-Down DP (Memoization) – O(n * k) Time and O(n * k) Space

It should be noted that the above function computes the same subproblems again and again. And have two properties of Dynamic Programming:

1. Optimal Substructure:

The value of C(n, k)depends on the optimal solutions of the subproblemsC(n-1, k-1) and C(n-1, k). By adding these optimal substrutures, we can efficiently calculate the total value of C(n, k).

2. Overlapping Subproblems:

While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times. Recursion tree for n = 5 and k = 2. The function C(3, 1) is called two times. For large values of n, there will be many common subproblems. 

binomial-coefficient

The Binomial Coefficient C(n, k) is computed recursively, but to avoid redundant calculations, dynamic programming with memoization is used. A 2D table stores previously computed values, allowing efficient lookups instead of recalculating. If a value is already computed, it is returned directly; otherwise, it is computed recursively and stored for future use.

C++
// C++ implementation to find 
// Binomial Coefficient using memoization

#include <bits/stdc++.h>
using namespace std;

// Returns value of Binomial Coefficient C(n, k)
int getnCk(int n, int k, vector<vector<int>> &memo) {
  
    // k can not be grater then k so we
  	// return 0 here
    if (k > n)
        return 0;
  
  	// base condition when k and n are 
  	// equal or k = 0
    if (k == 0 || k == n)
        return 1;
	
  	// Check if pair n and k is already 
  	// calculated then return it from here
  	if(memo[n][k] != -1) return memo[n][k];
  
    // Recurvie add the value and store to memorize table
    return memo[n][k] = getnCk(n - 1, k - 1, memo) 
      					+ getnCk(n - 1, k, memo);
}
int binomialCoeff(int n, int k) {
  	
  	// Create table for memorization
	vector<vector<int>> memo(n + 1, vector<int> (k + 1, -1));
  
  	return getnCk(n, k, memo);
}
int main() {
    int n = 5, k = 2;
    cout << binomialCoeff(n, k);
    return 0;
}
Java Python C# JavaScript

Output
10

Using Bottom-Up DP (Tabulation) – O(n * k) Time and O(n * k) Space

The approach is similar to the previous one. just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner. Maintain a dp[][] table such that dp[i][j] stores the count all unique possible paths to reach the cell (i, j).

Base Case:

  • For i = j and 0 <= i <= n , dp[i][j] = 1
  • for j = 0 and 0 <= j <= min(i, k) , dp[i][j] = 1

Recursive Case:

  • For i > 1 and j > 1 , dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
C++
// C++ implementation to find 
// Binomial Coefficient using tabulation

#include <bits/stdc++.h>
using namespace std;

// Returns value of Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k) {
  	vector<vector<int>> dp(n + 1, vector<int> (k + 1));
  
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= min(i, k); j++) {
          
            // Base Cases
            if (j == 0 || j == i)
                dp[i][j] = 1;

            // Calculate value using previously
            // stored values
            else
                dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
        }
    }

    return dp[n][k];
}

int main() {
    int n = 5, k = 2;
    cout << binomialCoeff(n, k);
}
C Java Python C# JavaScript

Output
10

Using Space Optimized DP – O(n * k) Time and O(k) Space

In the previous approach using dynamic programming, we derived a relation between states as follows:

  • dp[i][j] = dp[i-1][j-1]+dp[i-1][j]

We do not need to maitain whole matrix for this. We can just maintain one array of length k and add dp[j-1] every time to dp[j];

  • Use a 1D array dp[] of size k+1 to store binomial coefficients, reducing space complexity to O(k).
  • Set dp[0] = 1, representing nC0=1.
  • Update dp[j] in reverse order, using the previous values from the same array.
  • Each entry dp[j] is updated as dp[j] + dp[j-1] for each row.
  • The final value of dp(n,k) is stored in dp[k], and returned.
C++
// C++ program for space optimized Dynamic Programming
// Solution of Binomial Coefficient
#include <bits/stdc++.h>
using namespace std;

int binomialCoeff(int n, int k) {
    vector<int> dp(k + 1);

  	// nC0 is 1
    dp[0] = 1; 

    for (int i = 1; i <= n; i++) {
      
        // Compute next row of pascal triangle using
        // the previous row
        for (int j = min(i, k); j > 0; j--)
            dp[j] = dp[j] + dp[j - 1];
    }
    return dp[k];
}

int main() {
    int n = 5, k = 2;
    cout << binomialCoeff(n, k);
    return 0;
}
Java Python C# JavaScript

Output
10

Related articles:




Next Article

Similar Reads

three90RightbarBannerImg