Skip to content
Related Articles

Related Articles

Maximum of all the integers in the given level of Pascal triangle
  • Last Updated : 22 Nov, 2019

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




// 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
Output:
10

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :