Sum of product of Consecutive Binomial Coefficients


The problem statement includes printing the sum of product of consecutive binomial coefficients for any positive number, N which will be the user input.

The positive coefficients in the binomial expansion of any term are called binomial coefficients. These binomial coefficients can be found out using Pascal's triangle or a direct formula. The formula to calculate the binomial coefficient:

$$\mathrm{^nC_{r}=\frac{n!}{(n-r)!r!}}$$

where, n and r can be any positive numbers and r should never be greater than n.

Note : The value of 0! is always equal to 1.

In this problem, we will be given a positive number N and our task will be to find out the sum of product of consecutive binomial coefficients i.e. $\mathrm{\mathrm{^{i=N-1}{\sum_{i=0}}(^NC_{i}*^NC_{i+1})}}$

Let’s understand the problem with a few examples.

Example

Input

N=4

Output

56

Explanation − Here we are given N=4 and we not find the sum of product of consecutive binomial coefficients i.e $\mathrm{^{i=N-1}{\sum_{i=0}}(^NC_{i}*^NC_{i+1})}$

For N=4,

$\mathrm{(^4C_{0}*^4C_{1})+(^4C_{1}*^4C_{2})+(^4C_{2}*^4C_{3})+(^4C_{3}*^4C_{4})}$

Calculating the values of binomial coefficients using the formula, we get

(1*4)+(4*6)+(6*4)+(4*1)=4+24+24+4=56

The sum is 56 which is the output we require.

Input

N=1

Output

1

Explanation − The value of N is 1 in the input. We need to find the sum of the product of binomial coefficients from i=0 to i=N−1 i.e. 0.

So the required sum is, $\mathrm{^{i=N-1}{\sum_{i=0}}(^NC_{i}*^NC_{i+1})=(^1C_{0}*^1C_{1})=1*1=1}$

Let’s understand the algorithm to find the sum of products of consecutive binomial coefficients for any number N.

Algorithm

The process can be very lengthy if we calculate the value of binomial coefficients for each case and then finding the sum of product of consecutive binomial coefficients.

We will use the concept of Pascal’s triangle to calculate the value of binomial coefficients for a positive number N which will be given as an input.

Pascal's triangle is the array which is triangular and every value in the triangle represents the value of binomial coefficients. The pascal triangle can be demonstrated as below:

                                1
                            1        1
                        1        2        1
                      1      3         3       1
                   1     4       6        4        1  
                1     5     10       10        5       1

The triangle can be further expanded up to infinite using the same logic. Every number present in the pascal’s triangle represents the value of binomial coefficients where each row represents n starting from 0 and each column represents r starting from 0 in the formula $\mathrm{^nC_{r}}$

We will create a 2−D matrix of size (N+1)*(N+1) to store the values of binomial coefficients up to N, where N will be the positive number given in the input. To store the values of pascal’s triangle in a 2−D matrix, we will use the concepts of dynamic programming.

The steps below are the guide to store the values of pascal’s triangle in the 2−D matrix we created:

  • We will store 1 at mat[0][0] because $\mathrm{^0C_{0}}$ is equal to 1. For the remaining index of columns for the first row, we will remain the values to be 0 because r can never be greater than n in the formula $\mathrm{^nC_{r}}$

  • Iterate in a for loop from i=1 to i<N+1 since we need the values of binomial coefficients for N.

  • We will update mat[i][0] with 1 in every iteration as $\mathrm{^nC_{0}}$ is always equal to 1

  • Now we will iterate in a nested for loop for every value of i from j=1 to j<N+1 to calculate the value of binomial coefficients, $\mathrm{^nC_{r}}$ where r ranges from 0 to n.

  • There is a relation between the binomial coefficients which can be expressed as $\mathrm{^nC_{r}=^{n-1}C_{r-1}+^{n-1}C_{r}}$. For every value of i and j where 0<i,j<N+1 and i represents n and j represents r in the binomial coefficient, the value of mat[i][j] will be equal to mat[i−1][j−1]+mat[i−1][j].

  • We can update the pascal’s triangle until N in this way.

Once we get the pascal’s triangle we can simply take the required values of the binomial coefficient to find the sum of product of consecutive binomial coefficients.

The sum of products of consecutive binomial coefficients can be represented as $\mathrm{^{i=N-1}{\sum_{i=0}}(^NC_{i}*^NC_{i+1})}$. So to find the sum we can simply iterate in a for loop from i=0 to i<N because i ranges from 0 to N−1 and calculate the product of mat[N][i]*mat[N][i+1] and keep adding it in the sum which will give the resultant we require.

We will implement the above algorithm in our approach in order to solve the problem to find the sum of products of consecutive binomial coefficients.

Approach

The steps following which we can implement the above algorithm in our approach to find the sum of product of consecutive binomial coefficients:

  • For finding the sum, we will create a function.

  • We will create a 2−D vector of size (N+1)*(N+1) to store the values of pascal’s triangle in it.

  • In the function, we will update the 2−D vector with the values of Pascal’s triangle by following the steps discussed in the algorithm.

  • Once the vector is updated, we will then again iterate in a for loop to calculate the product of binomial coefficients from i=0 to $\mathrm{i<N(^{i=N-1}{\sum_{i=0}}(^NC_{i}*^NC_{i+1}))}$

  • We will keep updating the sum of product of binomial coefficients in a variable by mat[N][i]*mat[N][i+1].

  • We will return the variable as output which is the required sum.

Example

// C++ code for calculating sum of product of consecutive binomial binomial coefficients for N
#include <bits/stdc++.h>

using namespace std;

//function to store values of Pascal's triangle in 2d matrix
void pascaltriangle(int N,vector<vector<long long int>>& mat){
    mat[0][0]=1; //C(0,0)=1
    
    for(int i=1;i<N+1;i++){ //iterating in matrix to update the values of Pascal's triangle
    
        mat[i][0]=1;  //nC0 is always equal to 1
        
        for(int j=1;j<=i;j++){  //since r can never be greater than n in nCr
            
            mat[i][j] = mat[i-1][j-1] + mat[i-1][j];  //nCr= (n-1)C(r-1) + (n-1)Cr
            
        }
    }
}


int main()
{
    int N=18;
    
    vector<vector<long long int>> mat(N+1,vector<long long int>(N+1,0));  
     //initialise a 2d vector to store values of pascal triangle
    
    pascaltriangle(N,mat); //calling the function to update values in vector
    
   long long int ans=0; //variable to store sum of product of consecutive binomial coefficients
    for(int i=0;i<N;i++){
        ans = ans + (mat[N][i]*mat[N][i+1]); // (NCi*NCi+1) for i>=0 and i<=N-1
    }
    
    cout<<"The sum of product of consecutive binomial coefficients for N is : "<<ans<<endl;

    return 0;
}

Output

The sum of product of consecutive binomial coefficients for N is :       8597496600

Time complexity− O(N^2) , where N is the input as we iterate in a nested loop to update the values in the 2−D vector of size (N+1)*(N+1).

Space complexity − O(N^2) , because we created a 2−D matrix of size (N+1)*(N+1) to store the values of pascal’s triangle

Conclusion

The problem of printing the sum of products of consecutive binomial coefficients for any positive number N was discussed in the article. We used the concepts of Pascal’s triangle in our approach to find the values of all the binomial coefficients of N i.e. $\mathrm{^NC_{r}}$, where r ranges from 0 to N and find the product of consecutive binomial coefficients and sum them.

I hope you understand the problem and algorithm we used to solve it in C++ after reading this article.

Updated on: 28-Aug-2023

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements