A useful application of Pascal’s triangle is the calculation of combinations. The formula to find ** ^{n}C_{r}** is

**n! / r! * (n – r)!**which is also the formula for a cell of Pascal’s triangle.

**Pascal’s triangle:**

Input:n = 5, r = 3Output:10Explanation:n! / r! * (n - r)! = 5! / 3! * (2!) = 120 / 12 = 10Input:n = 7, r = 2Output:21Explanation:n! / r! * (n - r)! = 7! / 5! * (2!) = 42 / 2 = 21

**Approach:** The idea is to store the Pascal’s triangle in a matrix then the value of ** ^{n}C_{r}** will be the value of the cell at

**n**row and

^{th}**r**column.

^{th}*To create the pascal triangle use this two formula:*

, number of ways to select 0 elements from a set of n elements is 0^{n}C_{0}= 1, number of ways to select r elements from a set of n elements is summation of ways to select r-1 elements from n-1 elements and ways to select r elements from n-1 elements.^{n}C_{r}=^{n-1}C_{r-1}+^{n-1}C_{r}

The idea is to use the values of subproblems to calculate the answers for larger values. For example, to calculate ^{n}C_{r}, use the values of ^{n-1}C_{r-1} and ^{n-1}C_{r}. So DP can be used to preprocess all the values in the range.

**Algorithm:**

- Create a matrix of size 1000 * 1000, assign the value of base cases, i.e. run a loop from 0 to 1000 and assign matrix[i][0] = 1,
^{n}C_{0}= 1 - Run a nested loop from i = 1 to 1000 (outer loop) and the inner loop runs from j = 1 to i + 1.
- For every element (i, j) assign the value of
*matrix[i][j] = matrix[i-1][j-1] + matrix[i-1][j]*, using the formula^{n}C_{r}=^{n-1}C_{r-1}+^{n-1}C_{r} - After filling the matrix return the value of
^{n}C_{r}as matrix[n][r]

**Implementation:**

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Initialize the matrix with 0 ` `int` `l[1001][1001] = { 0 }; ` ` ` `int` `initialize() ` `{ ` ` ` ` ` `// 0C0 = 1 ` ` ` `l[0][0] = 1; ` ` ` `for` `(` `int` `i = 1; i < 1001; i++) { ` ` ` `// Set every nCr = 1 where r = 0 ` ` ` `l[i][0] = 1; ` ` ` `for` `(` `int` `j = 1; j < i + 1; j++) { ` ` ` ` ` `// Value for the current cell of Pascal's triangle ` ` ` `l[i][j] = (l[i - 1][j - 1] + l[i - 1][j]); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to return the value of nCr ` `int` `nCr(` `int` `n, ` `int` `r) ` `{ ` ` ` `// Return nCr ` ` ` `return` `l[n][r]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Build the Pascal's triangle ` ` ` `initialize(); ` ` ` `int` `n = 8; ` ` ` `int` `r = 3; ` ` ` `cout << nCr(n, r); ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `class` `GFG { ` ` ` `// Initialize the matrix with 0 ` ` ` `static` `int` `l[][] = ` `new` `int` `[` `1001` `][` `1001` `]; ` ` ` ` ` `static` `void` `initialize() ` ` ` `{ ` ` ` ` ` `// 0C0 = 1 ` ` ` `l[` `0` `][` `0` `] = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i < ` `1001` `; i++) { ` ` ` `// Set every nCr = 1 where r = 0 ` ` ` `l[i][` `0` `] = ` `1` `; ` ` ` `for` `(` `int` `j = ` `1` `; j < i + ` `1` `; j++) { ` ` ` ` ` `// Value for the current cell of Pascal's triangle ` ` ` `l[i][j] = (l[i - ` `1` `][j - ` `1` `] + l[i - ` `1` `][j]); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Function to return the value of nCr ` ` ` `static` `int` `nCr(` `int` `n, ` `int` `r) ` ` ` `{ ` ` ` `// Return nCr ` ` ` `return` `l[n][r]; ` ` ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Build the Pascal's triangle ` ` ` `initialize(); ` ` ` `int` `n = ` `8` `; ` ` ` `int` `r = ` `3` `; ` ` ` `System.out.println(nCr(n, r)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Initialize the matrix with 0 ` `l ` `=` `[[` `0` `for` `i ` `in` `range` `(` `1001` `)] ` `for` `j ` `in` `range` `(` `1001` `)] ` ` ` `def` `initialize(): ` ` ` ` ` `# 0C0 = 1 ` ` ` `l[` `0` `][` `0` `] ` `=` `1` ` ` `for` `i ` `in` `range` `(` `1` `, ` `1001` `): ` ` ` ` ` `# Set every nCr = 1 where r = 0 ` ` ` `l[i][` `0` `] ` `=` `1` ` ` `for` `j ` `in` `range` `(` `1` `, i ` `+` `1` `): ` ` ` ` ` `# Value for the current cell of Pascal's triangle ` ` ` `l[i][j] ` `=` `(l[i ` `-` `1` `][j ` `-` `1` `] ` `+` `l[i ` `-` `1` `][j]) ` ` ` `# Function to return the value of nCr ` `def` `nCr(n, r): ` ` ` `# Return nCr ` ` ` `return` `l[n][r] ` ` ` `# Driver code ` `# Build the Pascal's triangle ` `initialize() ` `n ` `=` `8` `r ` `=` `3` `print` `(nCr(n, r)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` ` ` `using` `System; ` `class` `GFG { ` ` ` `// Initialize the matrix with 0 ` ` ` `static` `int` `[, ] l = ` `new` `int` `[1001, 1001]; ` ` ` ` ` `static` `void` `initialize() ` ` ` `{ ` ` ` ` ` `// 0C0 = 1 ` ` ` `l[0, 0] = 1; ` ` ` `for` `(` `int` `i = 1; i < 1001; i++) { ` ` ` `// Set every nCr = 1 where r = 0 ` ` ` `l[i, 0] = 1; ` ` ` `for` `(` `int` `j = 1; j < i + 1; j++) { ` ` ` ` ` `// Value for the current cell of Pascal's triangle ` ` ` `l[i, j] = (l[i - 1, j - 1] + l[i - 1, j]); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Function to return the value of nCr ` ` ` `static` `int` `nCr(` `int` `n, ` `int` `r) ` ` ` `{ ` ` ` `// Return nCr ` ` ` `return` `l[n, r]; ` ` ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `// Build the Pascal's triangle ` ` ` `initialize(); ` ` ` `int` `n = 8; ` ` ` `int` `r = 3; ` ` ` `Console.WriteLine(nCr(n, r)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

**Output:**

56

**Complexity Analysis:**

**Time Complexity:**O(1).

The value of all pairs are precomputed so the time to answer the query is O(1), though some time is taken for precomputation but theoretically the precomputation takes constant time.**Space Commpelxity:**O(1).

Constant space is required.

## Recommended Posts:

- Program to calculate value of nCr
- Program to calculate the value of nCr Efficiently
- Compute nCr % p | Set 3 (Using Fermat Little Theorem)
- Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
- Compute nCr % p | Set 2 (Lucas Theorem)
- Sum of product of r and rth Binomial Coefficient (r * nCr)
- Queries of nCr%p in O(1) time complexity
- Find a pair from the given array with maximum nCr value
- Find if nCr is divisible by the given prime
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Program to calculate the Area and Perimeter of Incircle of an Equilateral Triangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Program to print a Hollow Triangle inside a Triangle
- Program to calculate the value of sin(x) and cos(x) using Expansion
- Program to calculate Resistance using given color code in circuits
- How to calculate the Easter date for a given year using Gauss' Algorithm
- Printing the Triangle Pattern using last term N
- Area of Circumcircle of an Equilateral Triangle using Median
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)

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.