Home > Backend Development > C++ > In C language, what is the maximum value of a binomial coefficient?

In C language, what is the maximum value of a binomial coefficient?

WBOY
Release: 2023-09-12 14:17:05
forward
1421 people have browsed it

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)!
Copy after login

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.

The method used in the following program is as follows

  • 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.

Example

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;
}
Copy after login

Output

If we run the above code, the following output will be generated −

Maximum Coefficient: 3
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template