Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Sum of all products of the Binomial Coefficients of two numbers up to K

  • Difficulty Level : Hard
  • Last Updated : 10 May, 2021

Given three integers N, M and K, the task is to calculate the sum of products of Binomial Coefficients C(N, i) and C(M, K – i), where i ranges between [0, K].

\begin{*align} \sum_{i=0}^{k}C(n, i)*C(m, k-i) \label{sum} \end{*align}

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Examples:



Input: N = 2, M = 2, K = 2 
Output:
Explanation: 
C(2, 0) * C(2, 2) + C(2, 1) * C(2, 1) + C(2, 2) * C(2, 0) = 1*1 + 2*2 +1*1 = 6
Input: N = 2, M = 3, K = 1 
Output:
Explanation: 
C(2, 0) * C(3, 1) + C(2, 1) * C(3, 0) = 1*3 + 2*1 = 5

Naive Approach:The simplest approach to solve this problem is to simply iterate over the range [0, K] and calculate C(N, i) and C(M, K – 1) for every i and update sum by adding their product. 
Below is the implementation of the above approach:

C++




// C++ implementation of
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function returns nCr
// i.e. Binomial Coefficient
int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i) {
 
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to calculate and
// return the sum of the products
int solve(int n, int m, int k)
{
 
    // Initialize sum to 0
    int sum = 0;
 
    // Traverse from 0 to k
    for (int i = 0; i <= k; i++)
        sum += nCr(n, i)
               * nCr(m, k - i);
 
    return sum;
}
 
// Driver Code
int main()
{
    int n = 3, m = 2, k = 2;
 
    cout << solve(n, m, k);
    return 0;
}

Java




// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Function returns nCr
// i.e. Binomial Coefficient
static int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to calculate and
// return the sum of the products
static int solve(int n, int m, int k)
{
 
    // Initialize sum to 0
    int sum = 0;
 
    // Traverse from 0 to k
    for (int i = 0; i <= k; i++)
        sum += nCr(n, i)
               * nCr(m, k - i);
 
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 3, m = 2, k = 2;
 
    System.out.print(solve(n, m, k));
}
}
 
// This code is contributed by Rohit_ranjan

Python3




# Python3 implementation of
# the above approach
 
# Function returns nCr
# i.e. Binomial Coefficient
def nCr(n, r):
     
    # Initialize res with 1
    res = 1
     
    # Since C(n, r) = C(n, n-r)
    if r > n - r:
        r = n - r
     
    # Evaluating expression
    for i in range(r):
        res *= (n - i)
        res /= (i + 1)
     
    return res;
     
# Function to calculate and
# return the sum of the products
def solve(n, m, k):
     
    # Initialize sum to 0
    sum = 0;
     
    # Traverse from 0 to k
    for i in range(k + 1):
        sum += nCr(n, i) * nCr(m, k - i)
     
    return int(sum)
     
# Driver code
if __name__ == '__main__':
     
    n = 3
    m = 2
    k = 2;
     
    print(solve(n, m, k))
 
# This code is contributed by jana_sayantan   

C#




// C# implementation of
// the above approach
using System;
class GFG{
 
// Function returns nCr
// i.e. Binomial Coefficient
static int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to calculate and
// return the sum of the products
static int solve(int n, int m, int k)
{
 
    // Initialize sum to 0
    int sum = 0;
 
    // Traverse from 0 to k
    for (int i = 0; i <= k; i++)
        sum += nCr(n, i)
            * nCr(m, k - i);
 
    return sum;
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 3, m = 2, k = 2;
 
    Console.Write(solve(n, m, k));
}
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
// JavaScript program for the above approach
 
// Function returns nCr
// i.e. Binomial Coefficient
function nCr(n, r)
{
  
    // Initialize res with 1
    let res = 1;
  
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
  
    // Evaluating expression
    for (let i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
  
    return res;
}
  
// Function to calculate and
// return the sum of the products
function solve(n, m, k)
{
  
    // Initialize sum to 0
    let sum = 0;
  
    // Traverse from 0 to k
    for (let i = 0; i <= k; i++)
        sum += nCr(n, i)
               * nCr(m, k - i);
  
    return sum;
}
     
// Driver Code
     
    let n = 3, m = 2, k = 2;
  
    document.write(solve(n, m, k));
              
</script>
Output: 
10

Time complexity: O(K2) 
Auxiliary Space: O(1)

Efficient Approach: 
The above approach can be optimized using Vandermonde’s Identity.

According to Vandermonde’s Identity, any combination of K items from a total of (N + M) items should have r items from M and (K – r) items from N items.

Therefore, the given expression is reduced to the following:

\begin{*align} \sum_{i=0}^{k}C(n, i)*C(m, k-i) = C(n+m, k) \label{sum} \end{*align}

Below is the implementation of the above approach:

C++




// C++ implementation of
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function returns nCr
// i.e. Binomial Coefficient
int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i) {
 
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Driver Code
int main()
{
    int n = 3, m = 2, k = 2;
 
    cout << nCr(n + m, k);
    return 0;
}

Java




// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Function returns nCr
// i.e. Binomial Coefficient
static int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 3, m = 2, k = 2;
 
    System.out.print(nCr(n + m, k));
}
}
 
// This code is contributed by sapnasingh4991

Python3




# Python3 implementation of
# the above approach
 
# Function returns nCr
# i.e. Binomial Coefficient
def nCr(n, r):
 
    # Initialize res with 1
    res = 1
 
    # Since C(n, r) = C(n, n-r)
    if(r > n - r):
        r = n - r
 
    # Evaluating expression
    for i in range(r):
        res *= (n - i)
        res //= (i + 1)
 
    return res
 
# Driver Code
if __name__ == '__main__':
 
    n = 3
    m = 2
    k = 2
 
    # Function call
    print(nCr(n + m, k))
 
# This code is contributed by Shivam Singh

C#




// C# implementation of
// the above approach
using System;
class GFG{
 
// Function returns nCr
// i.e. Binomial Coefficient
static int nCr(int n, int r)
{
 
    // Initialize res with 1
    int res = 1;
 
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
 
    // Evaluating expression
    for (int i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Driver Code
public static void Main()
{
    int n = 3, m = 2, k = 2;
    Console.Write(nCr(n + m, k));
}
}
 
// This code is contributed by Code_Mech

Javascript




<script>
 
// JavaScript implementation of the above approach
 
// Function returns nCr
// i.e. Binomial Coefficient
function nCr(n, r)
{
   
    // Initialize res with 1
    let res = 1;
   
    // Since C(n, r) = C(n, n-r)
    if (r > n - r)
        r = n - r;
   
    // Evaluating expression
    for (let i = 0; i < r; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Driver code
         
        let n = 3, m = 2, k = 2;
   
        document.write(nCr(n + m, k));
     
    // This code is contributed by code_hunt.
</script>
Output: 
10

Time Complexity: O(K) 
Auxiliary Space: O(1)
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!