# Find nCr

The problem of finding the value of nCr (n choose r) arises in various mathematical and combinatorial scenarios. nCr represents the number of ways to choose r elements from a set of n elements, where order does not matter. In this blog post, we will explore algorithms to solve this problem and provide implementations in C, C++, Python, and Java. Let’s delve into the details!

Algorithm:

The nCr value can be calculated using the following formula:

nCr = n! / (r! * (n-r)!)

However, calculating factorials directly can lead to large numbers and potential overflow. To avoid this, we can use the concept of Pascal’s triangle or dynamic programming to compute nCr efficiently.

Using Pascal’s Triangle:

- Create a 2D array, pascalTriangle, of size (n+1) x (n+1) and initialize all elements to 0.
- Set the first column of pascalTriangle to 1, as it represents nC0 = 1 for all values of n.
- For each row i from 1 to n:
- Set the first element of row i to 1, as it represents nCi = 1 for all values of i.
- For each column j from 1 to i:
- Calculate pascalTriangle[i][j] = pascalTriangle[i-1][j-1] + pascalTriangle[i-1][j].

- The value of nCr can be found at pascalTriangle[n][r].

Using Dynamic Programming:

- Create a 2D array, dp, of size (n+1) x (r+1) and initialize all elements to 0.
- Set dp[i][0] = 1 for all values of i from 0 to n, as nC0 = 1 for all values of n.
- For each row i from 1 to n and each column j from 1 to r:
- Calculate dp[i][j] = dp[i-1][j-1] + dp[i-1][j].

- The value of nCr can be found at dp[n][r].

Implementations:

- C:

#include <stdio.h> long long findNCR(int n, int r) { long long dp[n + 1][r + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= r; j++) { if (j == 0 || j == i) dp[i][j] = 1; else dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; } } return dp[n][r]; } int main() { int n = 5; int r = 3; long long ncr = findNCR(n, r); printf("%dC%d = %lldn", n, r, ncr); return 0; }

- C++:

#include <iostream> #include <vector> long long findNCR(int n, int r) { std::vector<std::vector<long long>> dp(n + 1, std::vector<long long>(r + 1, 0)); for (int i = 0; i <= n; i++) { for (int j = 0; j <= r; j++) { if (j == 0 || j == i) dp[i][j] = 1; else dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; } } return dp[n][r]; } int main() { int n = 5; int r = 3; long long ncr = findNCR(n, r); std::cout << n << "C" << r << " = " << ncr << std::endl; return 0; }

- Python:

def find_ncr(n, r): dp = [[0] * (r + 1) for _ in range(n + 1)] for i in range(n + 1): for j in range(r + 1): if j == 0 or j == i: dp[i][j] = 1 else: dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] return dp[n][r] n = 5 r = 3 ncr = find_ncr(n, r) print(f"{n}C{r} = {ncr}")

- Java:

public class NCR { public static long findNCR(int n, int r) { long[][] dp = new long[n + 1][r + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= r; j++) { if (j == 0 || j == i) dp[i][j] = 1; else dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; } } return dp[n][r]; } public static void main(String[] args) { int n = 5; int r = 3; long ncr = findNCR(n, r); System.out.println(n + "C" + r + " = " + ncr); } }

Conclusion:

Calculating the value of nCr, representing the number of ways to choose r elements from a set of n elements, can be efficiently achieved using Pascal’s triangle or dynamic programming. By utilizing these algorithms, we avoid potential overflow issues and calculate the value of nCr in an optimal manner. Implementations in C, C++, Python, and Java have been provided to demonstrate the algorithms. By understanding and utilizing these techniques, you will be equipped to solve problems that involve selecting combinations from a given set of elements.