Open In App

Height of Pyramid formed with given Rectangular Box

Last Updated : 20 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of rectangular blocks with dimensions {l, b} of length m. we can shape each rectangular block 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, placing 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 
Explanation: 
{8, 8} blocks 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 the height of the pyramid is 3

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

Approach: 

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

C++




#include <bits/stdc++.h>
#include <iostream>
 
using namespace std;
 
// Function to return
// The height of pyramid
int pyramid(int n, int arr[][2])
{
    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][0], arr[i][1]));
    }
    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[][2] = { { 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


Javascript




<script>
// Function to return
// The height of pyramid
function pyramid(n, arr) {
    let a = new Array();
    let height = 0;
    for (let i = 0; i < n; i++) {
        // For every ith array
        // append the minimum value
        // between two elements
        a.push(Math.min(arr[i][0], arr[i][1]));
    }
    a.sort((a, b) => a - b);
    for (let i = 0; i < a.length; 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
 
let n = 4;
let arr = [[8, 8],
[8, 2],
[4, 2],
[2, 1]];
document.write("Height of pyramid is " + pyramid(n, arr));
 
</script>


 
 

Output: 

Height of pyramid is  3

 

Time Complexity: O(n * log n)

Auxiliary Space: O(n)

 



Similar Reads

Largest square which can be formed using given rectangular blocks
Given an array arr[] of positive integers where each element of the array represents the length of the rectangular blocks. The task is to find the largest length of the square which can be formed using the rectangular blocks. Examples: Input: arr[] = {3, 2, 1, 5, 2, 4} Output: 3 Explanation: Using rectangular block of length 3, 5 and 4, square of s
10 min read
Print the pyramid pattern with given height and minimum number of stars
Given the minimum number of stars and the height of the pyramid, print the pattern.Examples: Input: min_stars = 1 p_height = 5 Output: * * *** *** ***** ***** ******* ******* ****************** Input: min_stars = 3 p_height = 7 Output: *** *** ***** ***** ******* ******* ********* ********* *********** *********** ************* ************* ******
8 min read
Find maximum height pyramid from the given array of objects
Given n objects, with each object has width wi. We need to arrange them in a pyramidal way such that : Total width of ith is less than (i + 1)th.Total number of objects in the ith is less than (i + 1)th. The task is to find the maximum height that can be achieved from given objects. Examples :Input : arr[] = {40, 100, 20, 30}Output : 2Top level : 3
11 min read
Pattern to print X in a rectangular box
Given the value of length, print the X pattern in a box using # and " " Examples: Input : 10 Output : ########## ## ## # # # # # # # # # ## # # ## # # # # # # # # # ## ## ########## Input : 7 Output : ####### ## ## # # # # # # # # # # # ## ## ####### Below is the implementation to print X in a rectangular box pattern: C/C++ Code // CPP code to prin
4 min read
Program to find slant height of cone and pyramid
Given two integers H1 and R representing the height and radius of a cone and two integers H2 and S representing the height and length of base of a pyramid, the task is to find the slant height of the cone and the pyramid. Examples: Input: H1 = 4.5, R = 6, H2 = 4, S = 4.8Output:Slant height of cone is: 7.5Slant height of pyramid is: 4.66476 Input: H
6 min read
Count of uncolored cells in a Pyramid of length H formed using cubes
Given a pyramid, formed using unit area cubes, of given height H. The pyramid is then placed on the ground and painted from outside. The task is to find the count of cubes that are left uncoloured. Examples: Input: H = 3Output: 1Explanation: Input: H = 1Output: 0 Naive Approach: The number of cubes that will remain uncoloured is the number of cubes
6 min read
Maximum height of an elevation possible such that adjacent matrix cells have a difference of at most height 1
Given a matrix mat][][] of size M x N which represents the topographic map of a region, and 0 denotes land and 1 denotes elevation, the task is to maximize the height in the matrix by assigning each cell a non-negative height such that the height of a land cell is 0 and two adjacent cells must have an absolute height difference of at most 1. Exampl
12 min read
Maximum of smallest possible area that can get with exactly k cut of given rectangular
Given n×m big rectangular area with unit squares and k times cut is allowed, the cut should be straight (horizontal or vertical) and should go along the edges of the unit square. What is the maximum possible area of the smallest piece he can get with exactly k cuts. Examples : Input : 3 4 1 Output : 6 Input : 6 4 2 Output : 8 Image for 2nd input Im
6 min read
Print rectangular pattern with given center
Given 3 positive integer c1, c2 and n, where n is size of 2-D square matrix. The task is to print the matrix filled with rectangular pattern having center coordinates c1, c2 such that 0 &lt;= c1, c2 &lt; n. Examples: Input: c1 = 2, c2 = 2, n = 5Output:2 2 2 2 2 2 1 1 1 2 2 1 0 1 2 2 1 1 1 2 2 2 2 2 2 Input: c1 = 3, c2 = 4, n = 7 Output:4 3 3 3 3 3
4 min read
Count tiles of dimensions 2 * 1 that can be placed in an M * N rectangular board that satisfies the given conditions
Given two integers M and N, the task is to find the minimum number of tiles of size 2 * 1 that can be placed on an M * N grid such that the following conditions are satisfied: Each tile must completely cover 2 squares of the board.No pair of tiles may overlap.Each tile lies must be placed entirely inside the board. It is allowed to touch the edges
4 min read