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

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].

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 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

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 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

Output:

10


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

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.