Box Stacking Problem in C++Last Updated : 17 Mar 2025 The box stacking problem is also well-known as the dynamic programming challenge. It asks the users to determine the highest stack of boxes that may be stacked on top of one another. The only way a box can be stacked atop another is if its base area is smaller, and even then, only when the box is rotated.
Code:Let's take an example to illustrate the box stacking problem in C++: Output: Input List elements are
4 7 6
1 3 2
4 6 5
10 32 12
Sorted List elements are
10 32 12
12 32 10
32 10 12
4 7 6
4 6 5
6 7 4
7 4 6
5 6 4
6 4 5
1 3 2
2 3 1
3 1 2
Max height is 60
Output list elements are
{3,2,1}
{1,2,3}
{6,5,4}
{4,5,6}
{4,6,7}
{32,12,10}
{10,12,32}
Using shared pointers and unique classes to solve the Box Stacking Problem. It includes a Box_Stacking class that manages the box stacking process and a dim_ension struct that represents box dimensions. The method uses dynamic programming to determine the highest box stacking height possible after sorting the boxes according to their base area. The output list of boxes that contribute to the maximum height is also prepared. The main method produces the maximum height along with a list of the boxes that contribute to it and uses a series of sample boxes to show how to use the Box_Stacking class. Next TopicC++ flat_map |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India