- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find maximum height pyramid from the given array of objects in C++

Suppose we have an array of n objects. Each object has width W[i]. We have to arrange them in a pyramidal way like −

Total width of ith is less than (i + 1)th

Total number of objects in the ith is less than (i + 1)th

For example, if the weights are like [40, 100, 20, 30], then output will be 2. So top level is 30, then lower level 20, 40 and 100

To solve this, we will use the greedy approach. The idea is to use place the objects with lower width at the top, the next object at the level right below and so on. To get the maximum number of levels, sort the given array and try to form pyramid from top to bottom.

Then find the smallest element of array like the first element of array after sorting, place it on the top. Then try to build levels below it with greater number of objects and the greater width.

## Example

#include <iostream> #include <algorithm> using namespace std; int maxLevelPyramid(int objects[], int n) { sort(objects, objects + n); int ans = 1; int prev_w = objects[0]; int count_p = 1; int count_c = 0; int curr_w = 0; for (int i=1; i<n; i++){ curr_w += objects[i]; count_c++; if (curr_w > prev_w && count_c > count_p){ prev_w = curr_w; count_p = count_c; count_c = curr_w = 0; ans++; } } return ans; } int main() { int boxes[] = {40, 100, 20, 30}; int n = sizeof(boxes)/sizeof(boxes[0]); cout << "Max level of pyramid: " << maxLevelPyramid(boxes, n); }

### Output

Max level of pyramid: 2

- Related Questions & Answers
- Find the sum of maximum difference possible from all subset of a given array in Python
- Find a pair from the given array with maximum nCr value in Python
- Find a pair from the given array with maximum nCr value in C++
- Maximum height of triangular arrangement of array values in C++
- Program to find maximum building height in Python
- Find maximum volume of a cuboid from the given perimeter and area in C++
- Search from an array of objects via array of string to get array of objects in JavaScript
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- C++ program to find out the maximum possible tally from given integers
- Write a Program to Find the Maximum Depth or Height of a Tree in C++
- Find the Initial Array from given array after range sum queries in C++
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- How to calculate the average in JavaScript of the given properties in the array of objects
- Maximum array from two given arrays keeping order same in C++
- Find maximum of minimum for every window size in a given array in C++