# Equal Stacks in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Equal Stacks in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

`Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.`

#### Introduction To Algorithm

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.

• It is easy to understand.
• Algorithm is a step-wise representation of a solution to a given problem.
• In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

Link for the ProblemEqual Stacks – Hacker Rank Solution

`Equal Stacks– Hacker Rank Solution`

### Problem:

You have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times.

Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of the three stacks until they are all the same height, then return the height.

Example

There are  and  cylinders in the three stacks, with their heights in the three arrays. Remove the top 2 cylinders from  (heights = [1, 2]) and from  (heights = [1, 1]) so that the three stacks all are 2 units tall. Return  as the answer.

Note: An empty stack is still a stack.

Function Description

Complete the equalStacks function in the editor below.

equalStacks has the following parameters:

• int h1[n1]: the first array of heights
• int h2[n2]: the second array of heights
• int h3[n3]: the third array of heights

Returns

• int: the height of the stacks when they are equalized

Input Format

The first line contains three space-separated integers, , , and , the numbers of cylinders in stacks , , and . The subsequent lines describe the respective heights of each cylinder in a stack from top to bottom:

• The second line contains  space-separated integers, the cylinder heights in stack . The first element is the top cylinder of the stack.
• The third line contains  space-separated integers, the cylinder heights in stack . The first element is the top cylinder of the stack.
• The fourth line contains  space-separated integers, the cylinder heights in stack . The first element is the top cylinder of the stack.

Constraints

Sample Input

```STDIN       Function
-----       --------
5 3 4       h1[] size n1 = 5, h2[] size n2 = 3, h3[] size n3 = 4
3 2 1 1 1   h1 = [3, 2, 1, 1, 1]
4 3 2       h2 = [4, 3, 2]
1 1 4 1     h3 = [1, 1, 4, 1]
```

Sample Output

```5
```

Explanation

Initially, the stacks look like this:

To equalize thier heights, remove the first cylinder from stacks  and , and then remove the top two cylinders from stack  (shown below).

The stack heights are reduced as follows:

`Equal Stacks – Hacker Rank Solution`
```import java.util.Scanner;
import java.util.Stack;

/**
* @author Techno-RJ
*
*/
public class EqualStacks {
static int equalStacks(int[] h1, int[] h2, int[] h3) {

Stack<Integer> st1 = new Stack<Integer>();
Stack<Integer> st2 = new Stack<Integer>();
Stack<Integer> st3 = new Stack<Integer>();

int st1TotalHeight = 0, st2TotalHeight = 0, st3TotalHeight = 0;

// pushing consolidated height into the stack instead of individual cylinder
// height
for (int i = h1.length - 1; i >= 0; i--) {
st1TotalHeight += h1[i];
st1.push(st1TotalHeight);
}
for (int i = h2.length - 1; i >= 0; i--) {
st2TotalHeight += h2[i];
st2.push(st2TotalHeight);
}
for (int i = h3.length - 1; i >= 0; i--) {
st3TotalHeight += h3[i];
st3.push(st3TotalHeight);
}

while (true) {

// If any stack is empty
if (st1.isEmpty() || st2.isEmpty() || st3.isEmpty())
return 0;

st1TotalHeight = st1.peek();
st2TotalHeight = st2.peek();
st3TotalHeight = st3.peek();

// If sum of all three stack are equal.
if (st1TotalHeight == st2TotalHeight && st2TotalHeight == st3TotalHeight)
return st1TotalHeight;

// Finding the stack with maximum sum and
// removing its top element.
if (st1TotalHeight >= st2TotalHeight && st1TotalHeight >= st3TotalHeight)
st1.pop();
else if (st2TotalHeight >= st1TotalHeight && st2TotalHeight >= st3TotalHeight)
st2.pop();
else if (st3TotalHeight >= st2TotalHeight && st3TotalHeight >= st1TotalHeight)
st3.pop();
}

}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n1 = in.nextInt();
int n2 = in.nextInt();
int n3 = in.nextInt();
int h1[] = new int[n1];
for (int h1_i = 0; h1_i < n1; h1_i++) {
h1[h1_i] = in.nextInt();
}
int h2[] = new int[n2];
for (int h2_i = 0; h2_i < n2; h2_i++) {
h2[h2_i] = in.nextInt();
}
int h3[] = new int[n3];
for (int h3_i = 0; h3_i < n3; h3_i++) {
h3[h3_i] = in.nextInt();
}
System.out.println(equalStacks(h1, h2, h3));
in.close();
}
}```  