
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Find a pair from the given array with maximum nCr value in C++
- Maximum height of triangular arrangement of array values in C++
- Find a pair from the given array with maximum nCr value in Python
- Maximum array from two given arrays keeping order same in C++
- Find the sum of maximum difference possible from all subset of a given array in Python
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- Find maximum volume of a cuboid from the given perimeter and area in C++
- Find maximum of minimum for every window size in a given array in C++
- C++ Program to find array after removal from maximum
- Find the Initial Array from given array after range sum queries in C++
- Maximum GCD from Given Product of Unknowns in C++
- Maximum size of sub-array that satisfies the given condition in C++
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Maximum Perimeter Triangle from array in C++
- C++ program to find out the maximum possible tally from given integers
