Sum of Product of r and rth Binomial Coefficient (r * nCr)


The problem states that we must determine the sum of the product of r and the rth binomial coefficient (r*nCr).

Positive numbers that are coefficients in the binomial theorem are called binomial coefficients. Both Pascal's triangle and a straightforward formula can be used to determine the binomial coefficients.

The formula for binomial coefficient is:

$$\mathrm{n_{c_{r}}=\frac{n!}{(n-r)!r!}}$$

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

In this equation n and r can be any non-negative integers and r should never be greater than n.

The objective at hand in this problem entails computing the product of the r and rth binomial coefficients as well as determining the total of all products from r=0 to r=n for any positive number n that the user inputs.

Let's examine the issue with a few illustrations:

Input

N=5

Output

80

Explanation : We calculated all the products of r and rth binomial coefficients and added them to obtain the output, where the value of r spans from 0 to N (i.e., r>=0 and r=N).

For r=0, 0 * $\mathrm{^5{C_{0}} = 0}$, calculating value of $\mathrm{^5{C_{0}}}$ using the formula mentioned above.

For r=1, 1 * $\mathrm{^5{C_{1}} = 5}$.

For r=2, 2 * $\mathrm{^5{C_{2}} = 20}$, since value of $\mathrm{^5{C_{2}} = 10}$ .

For r=3, 3 * $\mathrm{^5{C_{3}} = 30}$.

For r=4, 4 * $\mathrm{^5{C_{4}} = 20}$.

For r=5, 5 * $\mathrm{^5{C_{5}} = 5}$.

The binomial coefficients for r=0 to r=5 and their respective products, r and rth, are all listed above. Our desired output is 0+5+20+30+20+5=80, which is the sum of the products of the r and rth binomial coefficients.

Input

N=4

Output

32

Explanation: For all values of r in the range [0,4], sum of the product of the r and rth binomial coefficients is,

$$\mathrm{\sum(r*^N{C_{r}})}$$ for all r >=0 and r <=N

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

$$\mathrm{= 0 + 4 + 12 + 12 + 4 = 32}$$

Let's look at the approach to solve the problem.

Approach-1 (Pascal's triangle)

The idea behind Pascal's triangle to solve the problem is to get all the values of required binomial coefficients. As we know, Pascal's triangle depicts the values of binomial coefficients. The Pascal's triangle is demonstrated as below:

       1
      1 1
      1 2 1
     1 3 3 1
    1 4 6 4 1

This triangle can be further expanded using the same logic. Every value in Pascal's triangle represents the binomial coefficient starting from n=0 as rows and every value in the row represents $\mathrm{n_{c_{r}}}$, where r ranges from r=0 to r=n.

We will create a matrix of (N+1)*(N+1) size where N is any positive number given in the input and stores the values of Pascal's triangle in it. Using dynamic programming to store the values of Pascal's triangle in the matrix can make the process easy.

How to store values of Pascal's triangle in the matrix?

  • For the first row, we will store 1 at matrix[0][0] since $\mathrm{0_{c_{0}}= 1}$ . For the remaining columns for i=0, we will store 0 as r can never be greater than n in the expression $\mathrm{^n{C_{r}}}$.

  • For every value of i>0, store 1 at matrix[i][0] as $\mathrm{^n{C_{0}}}$ always equal to 1.

  • There is a relation for the binomial coefficients in mathematics which states $\mathrm{^n{C_{r}}=^{n-1}{C_{r-1}}+^{n-1}{C_{r}}}$. For every value of i and j, where 0<i<N+1 and 0<j<N+1 and i represents n and j represents r, we will store the value of matrix[i][j] with matrix[i-1][j-1] + matrix[i-1][j].

In this way we can fill the matrix with the values of Pascal's triangle where matrix[i][j] will represent the value of $\mathrm{^i{C_{j}}}$ .

We can calculate the sum of products of r and rth binomial coefficients using the matrix.

For using this approach to solve the problem, we have to follow the below steps:

  • Declare a matrix of size (N+1)*(N+1) to store the values of Pascal’s triangle.

  • We will make a function to store the values of Pascal’s triangle in the matrix using the above method.

  • Once the values are stored in the matrix, we can simply get the sum of product of r and rth binomial coefficients by iterating in a for loop.

  • Iterate in a for loop from i=0 to i<=N, and keep adding the product of every i and ith binomial coefficient in variable sum.

  • The value stored in the variable sum will be our output.

The C++ code for the approach is:

Example

// C++ code for calculating sum of product of r and rth 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;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=8;
   
   vector<vector<long long int>> mat(N+1,vector<long long int>(N+1,0));  
    //initialise a 2d vector to store values
   
   pascaltriangle(N,mat); //calling the function to update values in mat
   
   long long int sum=0; //variable to store sum of product of r and rth coefficients
   for(int i=0;i<=N;i++){
      sum = sum + (i * mat[N][i]); //mat[N][i] represents value of NCi
   }
   
   cout<<"The sum of product of r and rth binomial coefficients for N is "<<sum<<endl;

   return 0;
}

Output

The sum of product of r and rth binomial coefficients for N is 1024

Time Complexity : O(N^2)

Space Complexity : O(N^2)

Approach-2 (using direct formula)

We will be using a direct formula to calculate the sum of products of r and rth binomial coefficients where r lies in the range [0,N]. We will derive a formula to calculate the sum.

The sum of product of of r and rth binomial coefficients can be represented as:

$$\mathrm{\sum(r*^n{C_{r}})}$ $for all r>=0 and r <=N

So we need to find

$$\mathrm{\sum(r*^n{C_{r}})}$$

We can write r as$\mathrm{r_{C_{1}}}$ as $\mathrm{n_{C_{r}}}$ is always equals to n,

$$\mathrm{\sum(^r{C_{1}}*^n{C_{r}} )}$$

Using the formula$\mathrm{^n{C_{r}}=\frac{n!}{(n-r)!r!}}$,we can write the above expression as,

$$\mathrm{\sum(\frac{r!}{(r-1)!1!}*\frac{N!}{(N-r)!r!})=\sum(\frac{N!}{(N-r)!(r-1)!})=\sum N*(\frac{(N-1)!}{(N-r)!(r-1)!})}$$

Since $\mathrm{{n-1}_{c_{r-1}}=\frac{(N-1)!}{(N-1-(r-1))!(r-1)!}}$, which after simplifying can be written as $\mathrm{\frac{(N-1)!}{(N-r)!(r-1)!}}$

So replacing $\mathrm{\frac{(N-1)!}{(N-r)!(r-1)!}}$ with $\mathrm{{n-1}_{C_{r-1}}}$ the expression will be

$$\mathrm{N*\sum({n-1}_{C_{r-1}})=N*2^{N-1}(as \sum n_{c_{r}}=2^{N}, where 0<=r <=N)}$$

The sum of product of r and rth binomial coefficient can be calculated using the formula, $\mathrm{N*2^{N-1}}$for some positive value of N. The value of this expression can be calculated using the pow() function in C++

Syntax

int a=pow(2,N);

This will return the value equal to $\mathrm{2^{N}}$to a.

The C++ code for the problem using direct formula:

Example

#include<bits/stdc++.h>

using namespace std;

//function to calculate sum of product of r and rth binomial coefficients
long long int sum(int N){
   long long int ans=0; //to store the answer
   
   ans = N * pow(2,(N-1));  //using direct formula derived 
   
   return ans; // return the answer
}

int main()
{
   int N;
   
   N=4;
   cout<<"The sum of product of r and rth binomial coefficients is "<<sum(N)<<endl;
   
   N=15;
   cout<<"The sum of product of r and rth binomial coefficients is "<<sum(N)<<endl;
   

   return 0;
}

Output

The sum of product of r and rth binomial coefficients is 32
The sum of product of r and rth binomial coefficients is 245760

Time Complexity : O(log N) , because pow() function runs on O(log N) time complexity.

Space Complexity : O(1) , since we didn’t use any extra space.

Conclusion

The topic of calculating the sum of the r and rth binomial coefficients in C++ is addressed in the article using two different methods utilising several C++ functions and libraries. By establishing the formula to compute the output, we were able to convert the brute force method of issue solving to an effective method.

After reading this post, I hope that you have a good understanding of the issue.

Updated on: 21-Aug-2023

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements