# Height of Pyramid formed with given Rectangular Box

Given an array of rectangular blocks with dimensions {l, b} of length m. we can shape each of rectangular blocks into a single square block by trimming either one of its dimensions or both. With these square blocks a pyramid is formed. Our task is to find the maximum height of the pyramid that can be formed.
Note:
A pyramid is built by placing a square block, say N * N, at the base, place another square block N-1 * N-1 on top of that, a square block of N-2 * N-2 on top of that and so on, ending with a 1*1 block on top. The height of such a pyramid is N.

Examples:

Input: n = 4, arr[] = {{8, 8}, {2, 8}, {4, 2}, {2, 1}}
Output: Height of pyramid is 3
Explantion:
{8, 8} block can be trimmed to {3, 3}
{2, 8} block can be trimmed to {2, 2} or {4, 2} block can be trimmed to {2, 2}
{2, 1} block can be trimmed to {1, 1}
so height of pyramid is 3

Input: n = 5, arr[] = {{9, 8}, {7, 4}, {8, 1}, {4, 4}, {2, 1}}
Output: Height of pyramid is 4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• We have to make dimensions every block as
`l * l or b * b`
• Since we can only trim but not join, we choose dimensions of the square block as min(l, b)
• create an array a with the MIN(l, b) in every rectangular block
• sort the array a and initialize height to 0
• Traverse through array a if the element in array is greater than height + 1
then increment the value the value of height by 1
• return height

## C++

 `#include ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to return ` `// The height of pyramid ` `int` `pyramid(``int` `n, ``int` `arr[]) ` `{ ` `    ``vector<``int``> a; ` `    ``int` `height = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``// For every ith array ` `        ``// append the minimum value ` `        ``// between two elements ` `        ``a.push_back(min(arr[i], arr[i])); ` `    ``} ` `    ``sort(a.begin(), a.end()); ` `    ``for` `(``int` `i = 0; i < a.size(); i++) { ` `        ``// if the element in array is ` `        ``// greater than height then ` `        ``// increment the value the ` `        ``// value of height by 1 ` `        ``if` `(a[i] > height) ` `            ``height++; ` `    ``} ` `    ``return` `height; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 4; ` `    ``int` `arr[] = { { 8, 8 }, ` `                     ``{ 8, 2 }, ` `                     ``{ 4, 2 }, ` `                     ``{ 2, 1 } }; ` `    ``cout << ``"Height of pyramid is "` `         ``<< pyramid(n, arr); ` `} `

## Java

 `import` `java.util.*; ` ` `  `public` `class` `Gfg { ` ` `  `    ``static` `int` `pyramid(``int` `n, ``int` `arr[][]) ` `    ``{ ` `        ``int``[] a = ``new` `int``[n]; ` `        ``int` `height = ``0``; ` ` `  `        ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `            ``// For every ith array ` `            ``// append the minimum value ` `            ``// between two elements ` `            ``a[i] = arr[i][``0``] < arr[i][``1``] ` `                       ``? arr[i][``0``] ` `                       ``: arr[i][``1``]; ` `        ``} ` ` `  `        ``// sorting the array ` `        ``Arrays.sort(a); ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `            ``// if the element in array is ` `            ``// greater than height then ` `            ``// increment the value the ` `            ``// value of height by 1 ` `            ``if` `(a[i] > height) ` `                ``height++; ` `        ``} ` `        ``return` `height; ` `    ``} ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``4``; ` `        ``int` `arr[][] = { { ``8``, ``8` `}, ` `                        ``{ ``8``, ``2` `}, ` `                        ``{ ``4``, ``2` `}, ` `                        ``{ ``2``, ``1` `} }; ` `        ``System.out.println(``"Height of pyramid is "` `                           ``+ pyramid(n, arr)); ` `    ``} ` `} `

## Python

 `# Function to return ` `# The height of pyramid ` `def` `pyramid(n, arr): ` `    ``# Empty array ` `    ``a ``=` `[] ` `    ``height ``=` `0` `    ``for` `i ``in` `arr: ` `        ``# For every ith array ` `        ``# append the minimum value ` `        ``# between two elements ` `        ``a.append(``min``(i)) ` `    ``# Sort the array ` `    ``a.sort() ` `    ``# Traverse through the array a ` `    ``for` `i ``in` `range``(``len``(a)): ` `        ``# if the element in array is ` `        ``# greater than height then ` `        ``# increment the value the ` `        ``# value of height by 1 ` `        ``if` `a[i] > height: ` `            ``height ``=` `height ``+` `1` ` `  `    ``return` `height ` `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `4` `    ``arr ``=` `[[``8``, ``8``], [``2``, ``8``], [``4``, ``2``], [``2``, ``1``]] ` `    ``print``(``"Height of pyramid is"``, pyramid(n, arr)) `

## C#

 `using` `System; ` ` `  `class` `Gfg  ` `{  ` ` `  `    ``static` `int` `pyramid(``int` `n, ``int` `[,]arr)  ` `    ``{  ` `        ``int``[] a = ``new` `int``[n];  ` `        ``int` `height = 0;  ` ` `  `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{  ` ` `  `            ``// For every ith array  ` `            ``// append the minimum value  ` `            ``// between two elements  ` `            ``a[i] = arr[i, 0] < arr[i, 1]  ` `                    ``? arr[i, 0]  ` `                    ``: arr[i, 1];  ` `        ``}  ` ` `  `        ``// sorting the array  ` `        ``Array.Sort(a);  ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{  ` ` `  `            ``// if the element in array is  ` `            ``// greater than height then  ` `            ``// increment the value the  ` `            ``// value of height by 1  ` `            ``if` `(a[i] > height)  ` `                ``height++;  ` `        ``}  ` `        ``return` `height;  ` `    ``}  ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `n = 4;  ` `        ``int` `[,]arr = { { 8, 8 },  ` `                        ``{ 8, 2 },  ` `                        ``{ 4, 2 },  ` `                        ``{ 2, 1 } };  ` `         `  `        ``Console.WriteLine(``"Height of pyramid is "` `                        ``+ pyramid(n, arr));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by AnkitRai01 `

Output:

```Height of pyramid is  3
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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.

Improved By : AnkitRai01