Binomial Coefficient
Feb 22, 2022
Applications:
- Pascal’s Triangle
- Cases where the path/answer is f(i-1,j-1) + f(i-1,j) (since nCk = n-1Ck-1 + n-1Ck)
Complexity : O(n*k)
int binomialCoeff(int n, int k){int C[n + 1][k + 1];int i, j;// Calculate value of Binomial Coefficient// in bottom up mannerfor (i = 0; i <= n; i++) {for (j = 0; j <= min(i, k); j++) {// Base Casesif (j == 0 || j == i)C[i][j] = 1;// Calculate value using previously// stored valueselseC[i][j] = C[i - 1][j - 1] + C[i - 1][j];}}return C[n][k];}
Sample problem