Box Stacking Problem17 Mar 2025 | 4 min read Problem statementYou are given three integer arrays of size N, which represent the height, width and length of N boxes, respectively. Your task is to stack the boxes on each other such that the height should be maximum, and you have to return the height. To put one box on top of another box, the dimension of the top box should be strictly less than the dimension of the bottom box, which means the length and width of the top boxes should be strictly smaller than the bottom box. We can have multiple instances of a box, which means we can rotate the box as we want and then use it as another box. We know there are six faces of a cuboid. We can have six instances of a box, but there will be only three unique instances, so indirectly, we will have 3*N boxes with different dimensions, and we have to find the maximum height. ![]() Approach:We will use a dynamic programming approach here to solve this problem. We will sort the array based on its width, and then we will try to find out the longest Increasing Subsequence (LIS) to get the maximum height. Java Example:Output: ![]() Explanation: In the above code, we are given three different arrays representing the height, width and length of four different boxes. As we know, we can rotate the box to create its instance, so indirectly, we will have 12 options or 12 boxes for this problem. We will create the pair class to store the dimensions and write the comparator for our ArrayList. We will iterate over the array, and we will check three cases for each box to get its three instances. Case 1: If the width of the box is smaller than the length, then we will add as dimension as it is. Otherwise, we will swap width with length and add it as a new box. Case2: If the height of the box is smaller than the length, then we will swap the height and width; otherwise, we will swap the width with height and again height with length and add it as a new box. Case3: If the height of the box is smaller than the width, then we will swap width and length and then length and height to get the new dimension box. Otherwise, we will swap height with length and add it as a new box. After adding all the relevant boxes to ArrayList, we will sort them in ascending order according to their widths. We will use the LIS (Longest Increasing Subsequence) technique to get the solution. We will create the dp array of size the same as ArrayList. dp[i] will store the maximum height we can get by stacking the box from 0 to i-1 if the base of the stack is the ith box. If it is the first box, then the maximum height will be its height so that it will be a base case. Now for the rest of the boxes, we will loop from 0 to i-1 and get the maximum value of dp[j], and we will add the maximum value and height of the ith box to get the maximum size of the box. The maximum value of the dp array will be our answer. In the above example, we have four boxes so we will create its 12 instances as follows:
Time complexity: O(N2) Space complexity: O(N) Next TopicK Most Frequent Elements in Java |
Scoped value in Java
In Java, a scoped value refers to a variable that is defined within a specific block of code and is only accessible within that block and any nested blocks. The concept is crucial for maintaining code clarity, enting naming conflicts, and managing memory efficiently. In this...
3 min read
Threads in Java
Before introducing the thread concept, we were unable to run more than one task in parallel. It was a drawback, and to remove that drawback, the Thread Concept was introduced. A Thread is a very light-weighted process, or we can say the smallest part of the...
8 min read
Reduce Java
In Java, reducing is a terminal operation that aggregates a stream into a type or a primitive type. Java 8 provides Stream API contains set of predefined reduction operations such as average(), sum(), min(), max(), and count(). These operations return a value by combining the elements...
5 min read
Treeset Java Operations
TreeSet is a class in Java that implements the Set interface and is based on a tree data structure. It provides several operations to manage and manipulate a collection of elements in a sorted order. In this article, we will discuss the various TreeSet Java operations...
5 min read
Mirror Tree in Java
A refers to creating a mirrored version of a binary tree by swapping the left and right child nodes of every subtree. This process results in a symmetric reflection of the original tree structure. It is commonly solved using recursion or iterative approaches. Input: 1 2...
9 min read
Three-way operator | Ternary operator in Java
The one and only conditional operator that accepts three operands is the three-way operator in Java. Java programmers frequently use it as a one-line alternative to the if-then-else expression. The ternary operator can be used in place of if-else statements, and it can even be used...
3 min read
Convert Text-to-Speech in Java
Text-to-speech (TTS) or read-aloud is a type of assistive technology (it is a term for assistive, adaptive, and rehabilitative devices for people with disabilities) that reads digital text audibly. Converting text-to-speech (TTS) is an advanced functionality of smart devices like ATMs, online translators, text scanners, etc....
6 min read
Reverse A String and Reverse Every Alternative String in Java
String manipulation is a common task in programming, and Java provides various built-in methods and techniques to perform such operations efficiently. In this section, we will explore how to reverse a string and reverse every alternative substring within it using Java. Reversing a String: To reverse a string...
5 min read
Find Total Numbers with No Repeated Digits in A Range in Java
The problem of finding total numbers with no repeated digits in a range involves identifying numbers where each digit appears only once. It helps in analyzing number properties and is often used in combinatorics. This concept is useful in solving problems related to digit uniqueness and...
12 min read
Benefits of Learning Java
Java, a strong and flexible programming language, has long been a mainstay of the software development sector. Java has remained relevant and well-liked since its introduction in the middle of the 1990s, making it a great option for anybody wishing to enter the programming profession or...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India

