# 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]. 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 using namespace std; // Function returns nCr// i.e. Binomial Coefficientint 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 productsint 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 Codeint main(){    int n = 3, m = 2, k = 2;     cout << solve(n, m, k);    return 0;}

## Java

 // Java implementation of// the above approachimport java.util.*;class GFG{ // Function returns nCr// i.e. Binomial Coefficientstatic 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 productsstatic 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 Codepublic 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 Coefficientdef 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 productsdef 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 codeif __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 approachusing System;class GFG{ // Function returns nCr// i.e. Binomial Coefficientstatic 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 productsstatic 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 Codepublic 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

 
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: Below is the implementation of the above approach:

## C++

 // C++ implementation of// the above approach #include using namespace std; // Function returns nCr// i.e. Binomial Coefficientint 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 Codeint main(){    int n = 3, m = 2, k = 2;     cout << nCr(n + m, k);    return 0;}

## Java

 // Java implementation of// the above approachimport java.util.*;class GFG{ // Function returns nCr// i.e. Binomial Coefficientstatic 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 Codepublic 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 Coefficientdef 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 Codeif __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 approachusing System;class GFG{ // Function returns nCr// i.e. Binomial Coefficientstatic 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 Codepublic static void Main(){    int n = 3, m = 2, k = 2;    Console.Write(nCr(n + m, k));}} // This code is contributed by Code_Mech

## Javascript

 
Output:
10

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

My Personal Notes arrow_drop_up