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 coefficientint 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 triangleint findMax(int n){ return binomialCoeff(n, n / 2);}// Driver codeint main(){ int n = 5; cout << findMax(n); return 0;} |
Java
// Java implementation of the approachclass 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 approachusing 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 coefficientdef 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 triangledef findMax(n): return binomialCoeff(n, n // 2)# Driver coden = 5print(findMax(n))# This code is contributed by Mohit Kumar |
Javascript
<script>// Javascipt implementation of the// above approach// Function for the binomial coefficientfunction 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 trianglefunction findMax(n){ return binomialCoeff(n, Math.floor(n / 2));}// Driver codevar n = 5;document.write(binomialCoeff(n, Math.floor(n / 2)));// This code is contributed by ShubhamSingh10</script> |
10
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live


