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 : 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++
#include<bits/stdc++.h>
using namespace std;
int maxLevel( int boxes[], int n)
{
sort(boxes, boxes + n);
int ans = 1;
int prev_width = boxes[0];
int prev_count = 1;
int curr_count = 0;
int curr_width = 0;
for ( int i=1; i<n; i++)
{
curr_width += boxes[i];
curr_count += 1;
if (curr_width > prev_width &&
curr_count > prev_count)
{
prev_width = curr_width;
prev_count = curr_count;
curr_count = 0;
curr_width = 0;
ans++;
}
}
return ans;
}
int main()
{
int boxes[] = {10, 20, 30, 50, 60, 70};
int n = sizeof (boxes)/ sizeof (boxes[0]);
cout << maxLevel(boxes, n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG {
static int maxLevel( int []boxes, int n)
{
Arrays.sort(boxes);
int ans = 1 ;
int prev_width = boxes[ 0 ];
int prev_count = 1 ;
int curr_count = 0 ;
int curr_width = 0 ;
for ( int i = 1 ; i < n; i++)
{
curr_width += boxes[i];
curr_count += 1 ;
if (curr_width > prev_width &&
curr_count > prev_count)
{
prev_width = curr_width;
prev_count = curr_count;
curr_count = 0 ;
curr_width = 0 ;
ans++;
}
}
return ans;
}
static public void main (String[] args)
{
int []boxes = { 10 , 20 , 30 , 50 , 60 , 70 };
int n = boxes.length;
System.out.println(maxLevel(boxes, n));
}
}
|
Python 3
def maxLevel(boxes, n):
boxes.sort()
ans = 1
prev_width = boxes[ 0 ]
prev_count = 1
curr_count = 0
curr_width = 0
for i in range ( 1 , n):
curr_width + = boxes[i]
curr_count + = 1
if (curr_width > prev_width and
curr_count > prev_count):
prev_width = curr_width
prev_count = curr_count
curr_count = 0
curr_width = 0
ans + = 1
return ans
if __name__ = = "__main__" :
boxes = [ 10 , 20 , 30 , 50 , 60 , 70 ]
n = len (boxes)
print (maxLevel(boxes, n))
|
C#
using System;
public class GFG {
static int maxLevel( int []boxes, int n)
{
Array.Sort(boxes);
int ans = 1;
int prev_width = boxes[0];
int prev_count = 1;
int curr_count = 0;
int curr_width = 0;
for ( int i=1; i<n; i++)
{
curr_width += boxes[i];
curr_count += 1;
if (curr_width > prev_width &&
curr_count > prev_count)
{
prev_width = curr_width;
prev_count = curr_count;
curr_count = 0;
curr_width = 0;
ans++;
}
}
return ans;
}
static public void Main ()
{
int []boxes = {10, 20, 30, 50, 60, 70};
int n = boxes.Length;
Console.WriteLine(maxLevel(boxes, n));
}
}
|
PHP
<?php
function maxLevel( $boxes , $n )
{
sort( $boxes );
$ans = 1;
$prev_width = $boxes [0];
$prev_count = 1;
$curr_count = 0;
$curr_width = 0;
for ( $i = 1; $i < $n ; $i ++)
{
$curr_width += $boxes [ $i ];
$curr_count += 1;
if ( $curr_width > $prev_width and
$curr_count > $prev_count )
{
$prev_width = $curr_width ;
$prev_count = $curr_count ;
$curr_count = 0;
$curr_width = 0;
$ans ++;
}
}
return $ans ;
}
$boxes = array (10, 20, 30, 50, 60, 70);
$n = count ( $boxes );
echo maxLevel( $boxes , $n ) ;
?>
|
Javascript
<script>
function maxLevel(boxes, n)
{
boxes.sort();
let ans = 1;
let prev_width = boxes[0];
let prev_count = 1;
let curr_count = 0;
let curr_width = 0;
for (let i = 1; i < n; i++)
{
curr_width += boxes[i];
curr_count += 1;
if (curr_width > prev_width &&
curr_count > prev_count)
{
prev_width = curr_width;
prev_count = curr_count;
curr_count = 0;
curr_width = 0;
ans++;
}
}
return ans;
}
let boxes = [10, 20, 30, 50, 60, 70];
let n = boxes.length;
document.write(maxLevel(boxes, n));
</script>
|
Output :
3
Time Complexity : O(n log n).
Space Complexity: O(1) as no extra space has been taken.
Please see below article for more efficient solutions.
Maximum height of triangular arrangement of array values
This 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.