Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
 

Examples:  

Input: L = 3 
Output:
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++




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


Java




// 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


C#




// 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


Python3




# 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


Javascript




<script>
// Javascript implementation of the
// above approach
 
// Function for the binomial coefficient
function binomialCoeff(n, k)
{
 
    var C = new Array(n + 1);
    // Loop to create 2D array using 1D array
    for (var i = 0; i < C.length; i++) {
        C[i] = new Array(k + 1);
    }
    var 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
function findMax(n)
{
    return binomialCoeff(n, Math.floor(n / 2));
}
 
// Driver code
var n = 5;
document.write(binomialCoeff(n, Math.floor(n / 2)));
 
// This code is contributed by ShubhamSingh10
</script>


Output: 

10

 

Time Complexity: O(n2)

Auxiliary Space: O(n * k)



Last Updated : 10 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads