Maximum of all the integers in the given level of Pascal triangle

Given an integer L, the task is to find the maximum of all the integers present at the given level in Pascal’s triangle.
A Pascal triangle with 6 levels is shown below:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Examples:



Input: L = 3
Output: 3
0th level -> 1
1st level -> 1 1
2nd level -> 1 2 1
3rd level -> 1 3 3 1

Input: L = 5
Output: 10

Approach: It is known that each row in a Pascal Triangle is Binomial Coefficients and the kth coefficient in a binomial expansion for the level n is nCk. Also, the middle element of any level is always the greatest that is k = floor(n / 2).
Hence the maximum of all the integers present at the given level in Pascal’s triangle is binomialCoeff(n, n / 2).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function for the binomial coefficient
int binomialCoeff(int n, int k)
{
    int C[n + 1][k + 1];
    int i, j;
  
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (i = 0; i <= n; i++) {
        for (j = 0; j <= min(i, k); j++) {
  
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
  
            // Calculate value using previously
            // stored values
            else
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
  
    return C[n][k];
}
  
// Function to return the maximum
// value in the nth level
// of the Pascal's triangle
int findMax(int n)
{
    return binomialCoeff(n, n / 2);
}
  
// Driver code
int main()
{
    int n = 5;
  
    cout << findMax(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
  
class GFG
{
    // Function for the binomial coefficient
    static int binomialCoeff(int n, int k)
    {
        int [][] C = new int[n + 1][k + 1];
        int i, j;
      
        // Calculate value of Binomial Coefficient
        // in bottom up manner
        for (i = 0; i <= n; i++) {
            for (j = 0; j <= Math.min(i, k); j++) {
      
                // Base Cases
                if (j == 0 || j == i)
                    C[i][j] = 1;
      
                // Calculate value using previously
                // stored values
                else
                    C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
            }
        }
      
        return C[n][k];
    }
      
    // Function to return the maximum
    // value in the nth level
    // of the Pascal's triangle
    static int findMax(int n)
    {
        return binomialCoeff(n, n / 2);
    }
      
    // Driver code
    public static void main (String[] args) {
          
        int n = 5;
      
        System.out.println(findMax(n));
      
    }
  
}
  
  
// This code is contributed by ihritik

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
  
using System;
class GFG
{
    // Function for the binomial coefficient
    static int binomialCoeff(int n, int k)
    {
        int [ , ] C = new int[n + 1, k + 1];
        int i, j;
      
        // Calculate value of Binomial Coefficient
        // in bottom up manner
        for (i = 0; i <= n; i++) {
            for (j = 0; j <= Math.Min(i, k); j++) {
      
                // Base Cases
                if (j == 0 || j == i)
                    C[i, j] = 1;
      
                // Calculate value using previously
                // stored values
                else
                    C[i, j] = C[i - 1, j - 1] + C[i - 1, j];
            }
        }
      
        return C[n, k];
    }
      
    // Function to return the maximum
    // value in the nth level
    // of the Pascal's triangle
    static int findMax(int n)
    {
        return binomialCoeff(n, n / 2);
    }
      
    // Driver code
    public static void Main () {
          
        int n = 5;
      
        Console.WriteLine(findMax(n));
      
    }
  
}
  
  
// This code is contributed by ihritik

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function for the binomial coefficient
def binomialCoeff(n, k):
    C = [[0 for i in range(k + 1)] 
            for i in range(n + 1)]
  
    # Calculate value of Binomial Coefficient
    # in bottom up manner
    for i in range(n + 1):
        for j in range(min(i, k) + 1):
              
            # Base Cases
            if (j == 0 or j == i):
                C[i][j] = 1
  
            # Calculate value using previously
            # stored values
            else:
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j]
  
    return C[n][k]
  
# Function to return the maximum
# value in the nth level
# of the Pascal's triangle
def findMax(n):
    return binomialCoeff(n, n // 2)
  
# Driver code
n = 5
  
print(findMax(n))
  
# This code is contributed by Mohit Kumar

chevron_right


Output:

10



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.