Given three numbers n, r and p, compute value of nCr mod p.
Input: n = 10, r = 2, p = 13 Output: 6 Explanation: 10C2 is 45 and 45 % 13 is 6.
METHOD 1: (Using Dynamic Programming)
A Simple Solution is to first compute nCr, then compute nCr % p. This solution works fine when the value of nCr is small.
What if the value of nCr is large?
The value of nCr%p is generally needed for large values of n when nCr cannot fit in a variable, and causes overflow. So computing nCr and then using modular operator is not a good idea as there will be overflow even for slightly larger values of n and r. For example the methods discussed here and here cause overflow for n = 50 and r = 40.
The idea is to compute nCr using below formula
C(n, r) = C(n-1, r-1) + C(n-1, r) C(n, 0) = C(n, n) = 1
Working of Above formula and Pascal Triangle:
Let us see how above formula works for C(4, 3)
1==========>> n = 0, C(0, 0) = 1
1–1========>> n = 1, C(1, 0) = 1, C(1, 1) = 1
1–2–1======>> n = 2, C(2, 0) = 1, C(2, 1) = 2, C(2, 2) = 1
1–3–3–1====>> n = 3, C(3, 0) = 1, C(3, 1) = 3, C(3, 2) = 3, C(3, 3)=1
1–4–6–4–1==>> n = 4, C(4, 0) = 1, C(4, 1) = 4, C(4, 2) = 6, C(4, 3)=4, C(4, 4)=1
So here every loop on i, builds i’th row of pascal triangle, using (i-1)th row
Extension of above formula for modular arithmetic:
We can use distributive property of modulor operator to find nCr % p using above formula.
C(n, r)%p = [ C(n-1, r-1)%p + C(n-1, r)%p ] % p C(n, 0) = C(n, n) = 1
The above formula can implemented using Dynamic Programming using a 2D array.
The 2D array based dynamic programming solution can be further optimized by constructing one row at a time. See Space optimized version in below post for details.
Binomial Coefficient using Dynamic Programming
Below is implementation based on the space optimized version discussed in above post.
Value of nCr % p is 6
Time complexity of above solution is O(n*r) and it requires O(r) space. There are more and better solutions to above problem.
Compute nCr % p | Set 2 (Lucas Theorem)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
METHOD 2(Using Pascal Triangle and Dynamic Pro)
Another approach lies in utilizing the concept of the Pascal Triangle. Instead of calculating the nCr value for every n starting from n=0 till n=n, the approach aims at using the nth row itself for the calculation. The method proceeds by finding out a general relationship between nCr and nCr-1.
FORMULA: C(n,r)=C(n,r-1)* (n-r+1)/r Example: For instance, take n=5 and r=3. Input: n = 5, r = 3, p = 1000000007 Output: 6 Explanation: 5C3 is 10 and 10 % 100000007 is 10. As per the formula, C(5,3)=5!/(3!)*(2!) C(5,3)=10 Also, C(5,2)=5!/(2!)*(3!) C(5,2)=10 Let's try applying the above formula. C(n,r)=C(n,r-1)* (n-r+1)/r C(5,3)=C(5,2)*(5-3+1)/3 C(5,3)=C(5,2)*1 C(5,3)=10*1
The above example shows that C(n,r) can be easily calculated by calculating C(n,r-1) and multiplying the result with the term (n-r+1)/r. But this multiplication may cause integer overflow for large values of n. To tackle this situation, use modulo multiplication, and modulo division concepts in order to achieve optimizations in terms of integer overflow.
Let’s find out how to build Pascal Triangle for the same.
1D array declaration can be further optimized by just the declaration of a single variable to perform calculations. However, integer overflow demands other functions too for the final implementation.
The post below mentions the space and time-optimized implementation for the binary coefficient calculation.
Value of nCr % p is 10
- The above code needs an extra of O(1) space for the calculations.
- The time involved in the calculation of nCr % p is of the order O(n).
This article is improved by Aryan Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.