Home > Article > Backend Development > In C language, what is the maximum value of a binomial coefficient?
Given a positive integer ‘N’. We need to find the largest coefficient term among all binomial coefficients.
The binomial coefficient sequence is nC0, nC1, nC2,…,nCr,…,nCn-2,n Cn-1, nCn
Find the maximum value of nCr.
<sub>n</sub>C<sub>r</sub> = n! / r! * (n - r)!
Input - N=4
Output - Maximum coefficient - 6
Explanation - 4C0= 1, 4C1 = 4, 4C2 = 6 , 4C3 = 4, 4C4 = 1
Therefore, in this case, the maximum coefficient is 6.
Input - N=5
Output - Maximum coefficient - 10
Explanation - 5C0= 1, 5C1 = 5, 5C2 =10, 5C3 = 10, 5C4 = 5, 5C5 = 1
So, in this case, the maximum coefficient is 10.
We get the input of N from the user.
The function maxCoeff(int n) accepts one argument 'n' and returns the largest coefficient found so far in C[n 1][n 1].
Initialize the min and max variables with 0. 'min' is used to iterate over the C[][] array, and 'max' is used to store the maximum coefficient value found.
Use a loop of i from 0 to n to initialize the C[][] array.
Now traverse to the smaller of 'i' or 'n' in another loop.
If i==j, then C[i][j]==1. Otherwise, C[i][j] = C[i-1][j-1] C[i-1][j].
Now iterate through the entire C[][] again and store the maximum coefficient in max.
Return results.
Demonstration
#include <stdio.h> int maxCoeff(int n){ int C[n+1][n+1]; int max=0,min=0; // Calculate value of Binomial Coefficient in for (int i = 0; i <= n; i++){ min=i<n?i:n; for (int j = 0; j <= min; j++){ if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = C[i-1][j-1] + C[i-1][j]; } } for (int i = 0; i <= n; i++){ max = max> C[n][i] ? max: C[n][i]; } return max; } int main(){ int N = 3; printf("Maximum Coefficient :%d", maxCoeff(N) ); return 0; }
If we run the above code, the following output will be generated −
Maximum Coefficient: 3
The above is the detailed content of In C language, what is the maximum value of a binomial coefficient?. For more information, please follow other related articles on the PHP Chinese website!