![]() |
VOOZH | about |
Given two numbers n and r, The task is to find the value of nCr . Combinations represent the number of ways to choose r elements from a set of n distinct elements, without regard to the order in which they are selected. The formula for calculating combinations is :
Note: If r is greater than n, return 0.
Examples
Input: n = 5, r = 2
Output: 10
Explanation: The value of 5C2 is calculated as 5! / ((5−2)! * 2!)= 10.Input: n = 2, r = 4
Output: 0
Explanation: Since r is greater than n, thus 2C4 = 0Input: n = 5, r = 0
Output: 1
Explanation: The value of 5C0 is calculated as 5!/(5−0)!*0! = 5!/5!*0! = 1.
Table of Content
The idea is to use a recursive function to calculate the value of nCr. The base cases are:
- if r is greater than n, return 0 (there are no combinations possible)
- if r is 0 or r is n, return 1 (there is only 1 combination possible in these cases)
For other values of n and r, the function calculates the value of nCr by adding the number of combinations possible by including the current element and the number of combinations possible by not including the current element.
10
This approach calculates the nCr using the factorial formula. It first computes the factorial of a given number by multiplying all integers from 1 to that number.
To find nCr, it calculates the factorial of n, r, and (n - r) separately, then applies the formula n! / (r!(n-r)!) to determine the result. Since factorial values grow rapidly, this method is inefficient for large values due to integer overflow and excessive computations.
Note: This approach may produce incorrect results due to integer overflow when handling large values of n and r.
10
The formula for nCr is n! / (r!(n-r)!).
Instead of computing full factorials, we avoid redundant calculations by recognizing that r! and (n-r)! share common terms that cancel out.
To optimize, we compute the product of numbers from r+1 to n and divide it by the product of numbers from 1 to (n-r).
Here, r is chosen as the maximum of r and (n-r) to reduce the number of multiplications.
This approach avoids large factorial values, reducing computational overhead and preventing integer overflow.
Note: This approach may produce incorrect results due to integer overflow when handling large values of n and r.
10
A binomial coefficient C(n, k) can be defined as the coefficient of Xk in the expansion of (1 + X)n.
A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects; more formally, the number of k-element subsets (or k-combinations) of an n-element set.
Iterative way of calculating nCr using binomial coefficient formula.
10
Logarithmic formula for nCr is an alternative to the factorial formula that avoids computing factorials directly and it's more efficient for large values of n and r. It uses the identity log(n!) = log(1) + log(2) + ... + log(n) to express the numerator and denominator of the nCr in terms of sums of logarithms which allows to calculate the nCr using the Logarithmic operations. This approach is faster and very efficient.
The logarithmic formula for nCr is: nCr = exp( log(n!) - log(r!) - log((n-r)!))
10