# Sum of binomial coefficients (nCr) in a given range

Given three values, **N**, **L** and **R**, the task is to calculate the sum of binomial coefficients **( ^{n}C_{r})** for all values of

**r**from

**L**to

**R**.

**Examples:**

Input:N = 5, L = 0, R = 3Output:26Explanation:Sum of^{5}C_{0 }+^{5}C_{1 }+^{5}C_{2 }+^{5}C_{3}= 1 + 5 + 10 + 10 = 26.

Input:N = 3, L = 3, R = 3Output:1

**Approach:** Solve this problem by straightforward calculating ** ^{n}C_{r}** by using the formula

**n! / (r!(nâˆ’r)!)**and calculating factorial recursively for every value of

**r**from

**L**to

**R**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the factorial ` `// of a given number` `long` `long` `factorial(` `long` `long` `num)` `{` ` ` `if` `(num == 0 || num == 1)` ` ` `return` `1;` ` ` `else` ` ` `return` `num * factorial(num - 1);` `}` `// Function to calculate the sum ` `// of binomial coefficients(nCr) for ` `// all values of r from L to R` `long` `long` `sumOfnCr(` `int` `n, ` `int` `R, ` `int` `L)` `{` ` ` `long` `long` `r;` ` ` `long` `long` `res = 0;` ` ` `for` `(r = L; r <= R; r++)` ` ` `res += (factorial(n)` ` ` `/ (factorial(r) ` ` ` `* factorial(n - r)));` ` ` `return` `res;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 5, L = 0, R = 3;` ` ` `cout << sumOfnCr(N, R, L);` ` ` `return` `0;` `}` |

## Java

`// JAVA program for the above approach` `import` `java.io.*;` `class` `GFG {` ` ` `// Function to find the factorial` ` ` `// of a given number` ` ` `static` `long` `factorial(` `long` `num)` ` ` `{` ` ` `if` `(num == ` `0` `|| num == ` `1` `)` ` ` `return` `1` `;` ` ` `else` ` ` `return` `num * factorial(num - ` `1` `);` ` ` `}` ` ` `// Function to calculate the sum` ` ` `// of binomial coefficients(nCr) for` ` ` `// all values of r from L to R` ` ` `static` `long` `sumOfnCr(` `int` `n, ` `int` `R, ` `int` `L)` ` ` `{` ` ` `long` `r;` ` ` `long` `res = ` `0` `;` ` ` `for` `(r = L; r <= R; r++)` ` ` `res += (factorial(n)` ` ` `/ (factorial(r) * factorial(n - r)));` ` ` `return` `res;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `N = ` `5` `, L = ` `0` `, R = ` `3` `;` ` ` `long` `ans = sumOfnCr(N, R, L);` ` ` `System.out.println(ans);` ` ` `}` `}` `// This code is contributed by Taranpreet` |

## Python3

`# Python code for the above approach ` `# Function to find the factorial ` `# of a given number` `def` `factorial(num):` ` ` `if` `(num ` `=` `=` `0` `or` `num ` `=` `=` `1` `):` ` ` `return` `1` `;` ` ` `else` `:` ` ` `return` `num ` `*` `factorial(num ` `-` `1` `);` `# Function to calculate the sum ` `# of binomial coefficients(nCr) for ` `# all values of r from L to R` `def` `sumOfnCr(n, R, L):` ` ` `res ` `=` `0` `;` ` ` `for` `r ` `in` `range` `(L, R ` `+` `1` `):` ` ` `res ` `+` `=` `(factorial(n) ` `/` `(factorial(r) ` `*` `factorial(n ` `-` `r)));` ` ` `return` `res;` `# Driver Code` `N ` `=` `5` `L ` `=` `0` `R ` `=` `3` `;` `print` `((` `int` `)(sumOfnCr(N, R, L)))` `# This code is contributed by gfgking` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG {` ` ` `// Function to find the factorial` ` ` `// of a given number` ` ` `static` `long` `factorial(` `long` `num)` ` ` `{` ` ` `if` `(num == 0 || num == 1)` ` ` `return` `1;` ` ` `else` ` ` `return` `num * factorial(num - 1);` ` ` `}` ` ` `// Function to calculate the sum` ` ` `// of binomial coefficients(nCr) for` ` ` `// all values of r from L to R` ` ` `static` `long` `sumOfnCr(` `int` `n, ` `int` `R, ` `int` `L)` ` ` `{` ` ` `long` `r;` ` ` `long` `res = 0;` ` ` `for` `(r = L; r <= R; r++)` ` ` `res += (factorial(n)` ` ` `/ (factorial(r) * factorial(n - r)));` ` ` `return` `res;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `N = 5, L = 0, R = 3;` ` ` `Console.Write(sumOfnCr(N, R, L));` ` ` `}` `}` `// This code is contributed by ukasp.` |

## Javascript

`<script>` ` ` `// JavaScript code for the above approach ` ` ` `// Function to find the factorial ` ` ` `// of a given number` ` ` `function` `factorial(num) {` ` ` `if` `(num == 0 || num == 1)` ` ` `return` `1;` ` ` `else` ` ` `return` `num * factorial(num - 1);` ` ` `}` ` ` `// Function to calculate the sum ` ` ` `// of binomial coefficients(nCr) for ` ` ` `// all values of r from L to R` ` ` `function` `sumOfnCr(n, R, L) {` ` ` `let r;` ` ` `let res = 0;` ` ` `for` `(r = L; r <= R; r++)` ` ` `res += (factorial(n)` ` ` `/ (factorial(r)` ` ` `* factorial(n - r)));` ` ` `return` `res;` ` ` `}` ` ` `// Driver Code` ` ` `let N = 5, L = 0, R = 3;` ` ` `document.write(sumOfnCr(N, R, L));` ` ` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output**

26

**Time Complexity:**** **O(N * (R – L))**Auxiliary Space:**** **O(N)