Central binomial coefficient

Given an integer N, the task is to find the N^{th} Central binomial coefficient.
The first few Central binomial coefficients for N = 0, 1, 2, 3.. are

1, 2, 6, 20, 70, 252, 924, 3432…..

Examples:

Input: N = 3
Output: 20
Explanation:
N^{th} Central Binomial Cofficient = \binom{2N}{N} = \binom{2*3}{3} = \frac{6*5*4}{3*2*1} = 20

Input: N = 2
Output: 6



Approach: The central binomial coefficient is a binomial coefficient of the form \binom{2N}{N}. The Binomial Coefficient \binom{2N}{N} can be computed using this approach for a given value N using Dynamic Programming.

For Example:

Central binomial coefficient of N = 3 is given by:
\binom{2N}{N} = \binom{2*3}{3} = \frac{6*5*4}{3*2*1} = 20

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the 
// Nth Central Binomial Coefficient
  
#include<bits/stdc++.h> 
using namespace std; 
  
// Function to find the value of 
// Nth Central Binomial Coefficient
int binomialCoeff(int n, int k) 
    int C[n + 1][k + 1]; 
    int i, j; 
  
    // Calculate value of Binomial
    // Coefficient in bottom up manner 
    for (i = 0; i <= n; i++) 
    
        for (j = 0; j <= min(i, k); j++) 
        
            // Base Cases 
            if (j == 0 || j == i) 
                C[i][j] = 1; 
  
            // Calculate value 
            // using previously 
            // stored values 
            else
                C[i][j] = C[i - 1][j - 1] + 
                        C[i - 1][j]; 
        
    
  
    return C[n][k]; 
  
// Driver Code 
int main() 
    int n = 3;
    int k = n;
    n = 2*n;
    cout << binomialCoeff(n, k); 
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find the 
// Nth Central Binomial Coefficient
class GFG{
      
// Function to find the value of 
// Nth Central Binomial Coefficient
static int binomialCoeff(int n, int k) 
    int[][] C = new int[n + 1][k + 1]; 
    int i, j; 
  
    // Calculate value of Binomial
    // Coefficient in bottom up manner 
    for(i = 0; i <= n; i++) 
    
       for(j = 0; j <= Math.min(i, k); j++) 
       
             
          // Base Cases 
          if (j == 0 || j == i) 
              C[i][j] = 1
            
          // Calculate value 
          // using previously 
          // stored values 
          else
              C[i][j] = C[i - 1][j - 1] + 
                        C[i - 1][j]; 
       
    
    return C[n][k]; 
  
// Driver Code 
public static void main(String[] args)
    int n = 3;
    int k = n;
    n = 2 * n;
      
    System.out.println(binomialCoeff(n, k)); 
}
}
  
// This code is contributed by Ritik Bansal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find the 
// Nth Central Binomial Coefficient
using System;
class GFG{
      
// Function to find the value of 
// Nth Central Binomial Coefficient
static int binomialCoeff(int n, int k) 
    int [,]C = new int[n + 1, k + 1]; 
    int i, j; 
  
    // Calculate value of Binomial
    // Coefficient in bottom up manner 
    for(i = 0; i <= n; i++) 
    
       for(j = 0; j <= Math.Min(i, k); j++)
       {
             
          // Base Cases 
          if (j == 0 || j == i) 
              C[i, j] = 1; 
                
          // Calculate value 
          // using previously 
          // stored values 
          else
              C[i, j] = C[i - 1, j - 1] + 
                        C[i - 1, j]; 
       
    
    return C[n, k]; 
  
// Driver Code 
public static void Main()
    int n = 3;
    int k = n;
    n = 2 * n;
      
    Console.Write(binomialCoeff(n, k)); 
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

20

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : btc_148, Code_Mech, nidhi_biet