Maximum binomial coefficient term value in C

CServer Side ProgrammingProgramming

C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

We are given with a positive integer ‘N’. We have to find the maximum coefficient term in all binomial coefficients.

The binomial coefficient series is nC0, nC1, nC2, …., nCr, …., nCn-2, nCn-1, nCn

find the maximum value of nCr.

nCr = n! / r! * (n - r)!

Input − N=4

Output − Maximum Coefficient − 6

Explanation4C0= 1, 4C1 = 4, 4C2 = 6, 4C3 = 4, 4C4 = 1

Therefore, the maximum coefficient is 6 in this case.

Input − N=5

Output − Maximum Coefficient − 10

Explanation5C0= 1, 5C1 = 5, 5C2 =10, 5C3 = 10, 5C4 = 5, 5C5 = 1

Therefore, the maximum coefficient is 10 in this case.

Approach used in the below program is as follows

• We take input from the user for N.

• Function maxCoeff(int n) takes one parameter ‘n’ and return the maximum coefficient found so far stored in C[n+1][n+1]

• Initialize the min and max variables with 0. ‘min’ is used to traverse the C[][] array and ‘max’ is used to store the maximum coefficient value found.

• For loop from i=0 to n is used to initialize the C[][] array.

• Now inside another for loop traverse till ‘i’ or ‘n’ whichever is minimum.

• If i==j. C[i][j]==1. else C[i][j] = C[i-1][j-1] + C[i-1][j];

• Now traverse the whole C[][] again and store maximum coefficient in max.

• Return the result.

Example

Live Demo

#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;
}

Output

If we run the above code it will generate the following output −

Maximum Coefficient: 3
Updated on 17-Aug-2020 08:33:47