Sum of product of r and rth Binomial Coefficient (r * nCr) - GeeksforGeeks

25 Mar, 2021
Given a positive integer n. The task is to find the sum of the product of r and rth Binomial Coefficient. In other words find: Σ (r * nCr), where 0 <= r <= n.Examples:  
Input : n = 2
Output : 4
0.2C0 + 1.2C1 + 2.2C2
= 0*2 + 1*2 + 2*1
= 4

Input : n = 5
Output : 80
 
Method 1 (Brute Force) : The idea is to iterate a loop i from 0 to n and evaluate i * nCi and add to sum variable.Below is the implementation of this approach:  
C++
Java
Python 3
C#
PHP
Javascript
// CPP Program to find sum of product of r and// rth Binomial Coefficient i.e summation r * nCr#include <bits/stdc++.h>using namespace std;#define MAX 100 // Return the first n term of binomial coefficient.void binomialCoeff(int n, int C[]){    C[0] = 1; // nC0 is 1     for (int i = 1; i <= n; i++) {         // Compute next row of pascal triangle        // using the previous row        for (int j = min(i, n); j > 0; j--)            C[j] = C[j] + C[j - 1];    }} // Return summation of r * nCrint summation(int n){    int C[MAX];    memset(C, 0, sizeof(C));     // finding the first n term of binomial    // coefficient    binomialCoeff(n, C);     // Iterate a loop to find the sum.    int sum = 0;    for (int i = 0; i <= n; i++)        sum += (i * C[i]);        return sum;} // Driven Programint main(){    int n = 2;    cout << summation(n) << endl;    return 0;}
// Java Program to find sum// of product of r and rth// Binomial Coefficient i.e// summation r * nCrclass GFG{static int MAX = 100; // Return the first n term// of binomial coefficient.static void binomialCoeff(int n,                          int C[]){    C[0] = 1; // nC0 is 1     for (int i = 1; i <= n; i++)    {         // Compute next row of        // pascal triangle using        // the previous row        for (int j = Math.min(i, n); j > 0; j--)            C[j] = C[j] + C[j - 1];    }} // Return summation// of r * nCrstatic int summation(int n){    int C[] = new int[MAX];         for(int i = 0; i < MAX; i++)    C[i] = 0;     // finding the first n term     // of binomial coefficient    binomialCoeff(n, C);     // Iterate a loop    // to find the sum.    int sum = 0;    for (int i = 0; i <= n; i++)        sum += (i * C[i]);     return sum;} // Driver Codepublic static void main(String args[]){    int n = 2;    System.out.println( summation(n));}} // This code is contributed by Arnab Kundu
# Python 3 Program to find sum of product# of r and rth Binomial Coefficient i.e# summation r * nCrMAX = 100 # Return the first n term of# binomial coefficient.def binomialCoeff(n, C):     C[0] = 1 # nC0 is 1     for i in range(1, n + 1):         # Compute next row of pascal triangle        # using the previous row        for j in range(min(i, n), -1, -1):            C[j] = C[j] + C[j - 1] # Return summation of r * nCrdef summation( n):     C = [0] * MAX     # finding the first n term of    # binomial coefficient    binomialCoeff(n, C)     # Iterate a loop to find the sum.    sum = 0    for i in range(n + 1):        sum += (i * C[i])     return sum # Driver Codeif __name__ == "__main__":         n = 2    print(summation(n)) # This code is contributed by ita_c
// C# Program to find sum// of product of r and rth// Binomial Coefficient i.e// summation r * nCrusing System; class GFG{static int MAX = 100; // Return the first n term// of binomial coefficient.static void binomialCoeff(int n,                          int []C){    C[0] = 1; // nC0 is 1     for (int i = 1; i <= n; i++)    {         // Compute next row of        // pascal triangle using        // the previous row        for (int j = Math.Min(i, n);                 j > 0; j--)            C[j] = C[j] + C[j - 1];    }} // Return summation// of r * nCrstatic int summation(int n){    int []C = new int[MAX];         for(int i = 0; i < MAX; i++)    C[i] = 0;     // finding the first n term    // of binomial coefficient    binomialCoeff(n, C);     // Iterate a loop    // to find the sum.    int sum = 0;    for (int i = 0; i <= n; i++)        sum += (i * C[i]);     return sum;} // Driver Codepublic static void Main(){    int n = 2;    Console.Write( summation(n));}} // This code is contributed// by shiv_bhakt
<?php// PHP Program to find sum of product// of r and rth Binomial Coefficient// i.e summation r * nCr$MAX = 100; // Return the first n term of// binomial coefficient.function binomialCoeff($n, &$C){    $C[0] = 1; // nC0 is 1     for ($i = 1; $i <= $n; $i++)    {         // Compute next row of pascal triangle        // using the previous row        for ($j = min($i, $n); $j > 0; $j--)            $C[$j] = $C[$j] + $C[$j - 1];    }} // Return summation of r * nCrfunction summation($n){    global $MAX;    $C = array_fill(0, $MAX, 0);     // finding the first n term of    // binomial coefficient    binomialCoeff($n, $C);     // Iterate a loop to find the sum.    $sum = 0;    for ($i = 0; $i <= $n; $i++)        $sum += ($i * $C[$i]);     return $sum;} // Driver Code$n = 2;echo summation($n); // This code is contributed by mits?>
<script>     // Javascript Program to find sum of product of r and    // rth Binomial Coefficient i.e summation r * nCr    MAX = 100     // Return the first n term of binomial coefficient.    function binomialCoeff(n, C) {      C[0] = 1; // nC0 is 1       for (var i = 1; i <= n; i++) {         // Compute next row of pascal triangle        // using the previous row        for (var j = Math.min(i, n); j > 0; j--)          C[j] = C[j] + C[j - 1];      }    }     // Return summation of r * nCr    function summation(n) {      C = Array(MAX).fill(0);       // finding the first n term of binomial      // coefficient      binomialCoeff(n, C);       // Iterate a loop to find the sum.      var sum = 0;      for (var i = 0; i <= n; i++)        sum += (i * C[i]);       return sum;    }     // Driven Program    var n = 2;    document.write(summation(n));         // This code is contributed by noob2000.  </script>
4
 
Method 2 (Using formula) : Mathematically we need to find, Σ (i * nCi), where 0 <= i <= n = Σ (iC1 * nCi), (Since nC1 = n, we can write i as iC1) = Σ ( (i! / (i – 1)! * 1!) * (n! / (n – i)! * i!) On cancelling i! from numerator and denominator = Σ (n! / (i – 1)! * (n – i)!) = Σ n * ((n – 1)!/ (i – 1)! * (n – i)!) (Using reverse of nCr = (n)!/ (r)! * (n – r)!) = n * Σ n – 1Cr – 1 = n * 2n – 1 (Since Σ nCr = 2n)Below is the implementation of this approach:  
C++
Java
Python3
C#
PHP
Javascript
// CPP Program to find sum of product of r and// rth Binomial Coefficient i.e summation r * nCr#include <bits/stdc++.h>using namespace std;#define MAX 100 // Return summation of r * nCrint summation(int n){    return n << (n - 1);} // Driven Programint main(){    int n = 2;    cout << summation(n) << endl;    return 0;}
// Java Program to find sum of product of// r and rth Binomial Coefficient i.e// summation r * nCrimport java.io.*; class GFG {     static int MAX = 100;         // Return summation of r * nCr    static int summation(int n)    {        return n << (n - 1);    }         // Driven Program    public static void main (String[] args)    {        int n = 2;        System.out.println( summation(n));    }} // This code is contributed by anuj_67.
# Python3 Program to# find sum of product# of r and rth Binomial# Coefficient i.e# summation r * nCr # Return summation# of r * nCrdef summation( n):    return n << (n - 1); # Driver Coden = 2;print(summation(n)); # This code is contributed# by mits
// C# Program to find sum of product of// r and rth Binomial Coefficient i.e// summation r * nCrusing System; class GFG {     //static int MAX = 100;         // Return summation of r * nCr    static int summation(int n)    {        return n << (n - 1);    }         // Driver Code    public static void Main ()    {        int n = 2;        Console.WriteLine( summation(n));    }} // This code is contributed by anuj_67.
<?php// PHP Program to find sum// of product of r and// rth Binomial Coefficient// i.e summation r * nCr // Return summation of r * nCrfunction summation( $n){    return $n << ($n - 1);} // Driver Code$n = 2;echo summation($n) ; // This code is contributed// by shiv_bhakt?>
<script> // Javascript Program to find sum of product of r and// rth Binomial Coefficient i.e summation r * nCrvar MAX=100 // Return summation of r * nCrfunction summation(n){    return n << (n - 1);} // Driven Programvar n = 2;document.write(summation(n));     </script>
4
 
vt_m
andrew1234
Vishal_Khoda
Mithun Kumar
ukasp
rutvik_56
noob2000
binomial coefficient
Dynamic Programming
Mathematical
Dynamic Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Optimal Substructure Property in Dynamic Programming | DP-2
Optimal Binary Search Tree | DP-24
Min Cost Path | DP-6
Maximum Subarray Sum using Divide and Conquer algorithm
Greedy approach vs Dynamic programming
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples