Find maximum height pyramid from the given array of objects

24 Nov, 2021
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.
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 : 2
Top level : 30.
Lower (or bottom) level : 20, 40 and 100
Other possibility can be placing
20 on the top, and at second level any
other 4 objects. Another possibility is
to place 40 at top and other three at the
bottom.

Input : arr[] = {10, 20, 30, 50, 60, 70}
Output : 3
 
The idea is to use greedy approach by placing the object with the lowest width at the top, the next object at the level right below and so on. To find the maximum number of levels, sort the given array and try to form pyramid from top to bottom. Find the smallest element of array i.e first element of array after sorting, place it on the top. Then try to build levels below it with greater number of objects and greater width.Below is the implementation of this approach:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to find maximum height pyramid// from the given object width.#include<bits/stdc++.h>using namespace std;  // Returns maximum number of pyramidcal levels// n boxes of given widths.int maxLevel(int boxes[], int n){    // Sort objects in increasing order of widths    sort(boxes, boxes + n);      int ans = 1;  // Initialize result      // Total width of previous level and total    // number of objects in previous level    int prev_width = boxes[0];    int prev_count = 1;      // Number of object in current level.    int curr_count = 0;      // Width of current level.    int curr_width = 0;    for (int i=1; i<n; i++)    {        // Picking the object. So increase current        // width and number of object.        curr_width += boxes[i];        curr_count += 1;          // If current width and number of object        // are greater than previous.        if (curr_width > prev_width &&            curr_count > prev_count)        {            // Update previous width, number of            // object on previous level.            prev_width = curr_width;            prev_count = curr_count;              // Reset width of current level, number            // of object on current level.            curr_count = 0;            curr_width = 0;              // Increment number of level.            ans++;        }    }      return ans;}  // Driver Programint main(){    int boxes[] = {10, 20, 30, 50, 60, 70};    int n = sizeof(boxes)/sizeof(boxes[0]);    cout << maxLevel(boxes, n) << endl;    return 0;}
// Java program to find maximum height pyramid// from the given object width.import java.io.*;import java.util.Arrays;  class GFG {          // Returns maximum number of pyramidcal    // levels n boxes of given widths.    static int maxLevel(int []boxes, int n)    {          // Sort objects in increasing order        // of widths        Arrays.sort(boxes);              int ans = 1; // Initialize result              // Total width of previous level         // and total number of objects in        // previous level        int prev_width = boxes[0];        int prev_count = 1;              // Number of object in current        // level.        int curr_count = 0;              // Width of current level.        int curr_width = 0;        for (int i = 1; i < n; i++)        {            // Picking the object. So            // increase current width            // and number of object.            curr_width += boxes[i];            curr_count += 1;                  // If current width and             // number of object            // are greater than previous.            if (curr_width > prev_width &&                curr_count > prev_count)            {                                  // Update previous width,                // number of object on                 // previous level.                prev_width = curr_width;                prev_count = curr_count;                      // Reset width of current                // level, number of object                 // on current level.                curr_count = 0;                curr_width = 0;                      // Increment number of                // level.                ans++;            }        }              return ans;    }          // Driver Program    static public void main (String[] args)    {        int []boxes = {10, 20, 30, 50, 60, 70};        int n = boxes.length;        System.out.println(maxLevel(boxes, n));    }}  // This code is contributed by anuj_67.
# Python 3 program to find # maximum height pyramid from # the given object width.  # Returns maximum number # of pyramidcal levels n # boxes of given widths.def maxLevel(boxes, n):          # Sort objects in increasing    # order of widths    boxes.sort()      ans = 1 # Initialize result      # Total width of previous     # level and total number of     # objects in previous level    prev_width = boxes[0]    prev_count = 1      # Number of object in    # current level.    curr_count = 0      # Width of current level.    curr_width = 0    for i in range(1, n):          # Picking the object. So         # increase current width         # and number of object.        curr_width += boxes[i]        curr_count += 1          # If current width and         # number of object are         # greater than previous.        if (curr_width > prev_width and            curr_count > prev_count):              # Update previous width,             # number of object on             # previous level.            prev_width = curr_width            prev_count = curr_count              # Reset width of current             # level, number of object             # on current level.            curr_count = 0            curr_width = 0              # Increment number of level.            ans += 1    return ans  # Driver Codeif __name__ == "__main__":    boxes= [10, 20, 30, 50, 60, 70]    n = len(boxes)    print(maxLevel(boxes, n))  # This code is contributed # by ChitraNayal
// C# program to find maximum height pyramid// from the given object width.using System;  public class GFG {          // Returns maximum number of pyramidcal    // levels n boxes of given widths.    static int maxLevel(int []boxes, int n)    {        // Sort objects in increasing order        // of widths        Array.Sort(boxes);              int ans = 1; // Initialize result              // Total width of previous level         // and total number of objects in        // previous level        int prev_width = boxes[0];        int prev_count = 1;              // Number of object in current        // level.        int curr_count = 0;              // Width of current level.        int curr_width = 0;        for (int i=1; i<n; i++)        {            // Picking the object. So            // increase current width            // and number of object.            curr_width += boxes[i];            curr_count += 1;                  // If current width and             // number of object            // are greater than previous.            if (curr_width > prev_width &&                   curr_count > prev_count)            {                                  // Update previous width,                // number of object on                 // previous level.                prev_width = curr_width;                prev_count = curr_count;                      // Reset width of current                // level, number of object                 // on current level.                curr_count = 0;                curr_width = 0;                      // Increment number of                // level.                ans++;            }        }              return ans;    }          // Driver Program    static public void Main ()    {        int []boxes = {10, 20, 30, 50, 60, 70};        int n = boxes.Length;        Console.WriteLine(maxLevel(boxes, n));    }}  // This code is contributed by anuj_67.
<?php// PHP program to find maximum // height pyramid from the// given object width.  // Returns maximum number of // pyramidcal levels n boxes // of given widths.function maxLevel($boxes, $n){    // Sort objects in increasing     // order of widths    sort($boxes);      // Initialize result    $ans = 1;       // Total width of previous     // level and total number     // of objects in previous level    $prev_width = $boxes[0];    $prev_count = 1;      // Number of object     // in current level.    $curr_count = 0;      // Width of current level.    $curr_width = 0;    for ( $i = 1; $i < $n; $i++)    {        // Picking the object. So         // increase current width         // and number of object.        $curr_width += $boxes[$i];        $curr_count += 1;          // If current width and number        // of object are greater         // than previous.        if ($curr_width > $prev_width and            $curr_count > $prev_count)        {            // Update previous width, number             // of object on previous level.            $prev_width = $curr_width;            $prev_count = $curr_count;              // Reset width of current            // level, number of object            // on current level.            $curr_count = 0;            $curr_width = 0;              // Increment number of level.            $ans++;        }    }    return $ans;}  // Driver Code$boxes = array(10, 20, 30, 50, 60, 70);$n = count($boxes);echo maxLevel($boxes, $n) ;  // This code is contributed by anuj_67.?>
<script>  // JavaScript program for the above approach      // Returns maximum number of pyramidcal    // levels n boxes of given widths.    function maxLevel(boxes, n)    {          // Sort objects in increasing order        // of widths        boxes.sort();              let ans = 1; // Initialize result              // Total width of previous level         // and total number of objects in        // previous level        let prev_width = boxes[0];        let prev_count = 1;              // Number of object in current        // level.        let curr_count = 0;              // Width of current level.        let curr_width = 0;        for (let i = 1; i < n; i++)        {            // Picking the object. So            // increase current width            // and number of object.            curr_width += boxes[i];            curr_count += 1;                  // If current width and             // number of object            // are greater than previous.            if (curr_width > prev_width &&                curr_count > prev_count)            {                                  // Update previous width,                // number of object on                 // previous level.                prev_width = curr_width;                prev_count = curr_count;                      // Reset width of current                // level, number of object                 // on current level.                curr_count = 0;                curr_width = 0;                      // Increment number of                // level.                ans++;            }        }              return ans;    }  // Driver Code    let boxes = [10, 20, 30, 50, 60, 70];    let n = boxes.length;    document.write(maxLevel(boxes, n));  // This code is contributed by susmitakundugoaldanga.</script>
Output :  
3
Time Complexity : O(n log n).Please see below article for more efficient solutions. Maximum height of triangular arrangement of array valuesThis article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
ukasp
susmitakundugoaldanga
Greedy
Sorting
Greedy
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for array rotation
Write a program to print all permutations of a given string
Coin Change | DP-7
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
Minimum Number of Platforms Required for a Railway/Bus Station
Merge Sort
Bubble Sort Algorithm
QuickSort
Insertion Sort
Selection Sort Algorithm