Equal Stacks in C#

Problem Source – Equal Stacks – Hackerrank

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’re all the same height, then print the height. The removals must be performed in such a way as to maximize the height.

Note: An empty stack is still a stack.

Input Format:

The first line contains three space-separated integers, n1, n2, and n3, describing the respective number of cylinders in stacks 1, 2, and 3. The subsequent lines describe the respective heights of each cylinder in a stack from top to bottom:

• The second line contains n1 space-separated integers describing the cylinder heights in stack 1.
• The third line contains n2  space-separated integers describing the cylinder heights in stack 2.
• The fourth line contains n3  space-separated integers describing the cylinder heights in stack 3.

Constraints:

0 < n1, n2, n3 <= 10^5

0 < height of any cylinder <= 100

Output Format:

Print a single integer denoting the maximum height at which all stacks will be of equal height.

Sample Input:

5 3 4
3 2 1 1 1
4 3 2
1 1 4 1


Sample Output

5


Explanation:

Initially, the stacks look like this:

Observe that the three stacks are not all the same height. To make all stacks of equal height, we remove the first cylinder from stacks 1 and 2, and then remove the top two cylinders from stack 3(shown below).

As a result, the stacks undergo the following change in height:

1. 8 – 3 = 5
2. 9 – 4 = 5
3. 7 – 1 – 1 = 5

Algorithm:

As our task is to find maximum possible height of stacks that means maximum height is common height of all the stacks. Below are the steps to compute common height for all the stacks:

1. First get the sum of all the cylinders of each stack and hold this sum in separate variables i.e. sum1, sum2, sum3.
2. Start looping till sum1, sum2 and sum3 are not equal.
1. check if height of stack1 is higher than other stacks then pop top cylinder and reduce sum1 by that cylinder weight.
2. check if height of stack2 is higher than other stacks then pop top cylinder and reduce sum2 by that cylinder weight.
3. check if height of stack3 is higher than other stacks then pop top cylinder and reduce sum3 by that cylinder weight.
3. Print the sum1. If there is nothing common the it’s value will be 0, otherwise it’ll have common height.

Code in C#:

static void Main(string[] args)
{
var n1 = Convert.ToInt32(tokens_n1[0]);
var n2 = Convert.ToInt32(tokens_n1[1]);
var n3 = Convert.ToInt32(tokens_n1[2]);

var h1_temp = new string[n1];
var h2_temp = new string[n2];
var h3_temp = new string[n3];

var st1 = new Stack<int>();
var st2 = new Stack<int>();
var st3 = new Stack<int>();

int sum1 = 0, sum2 = 0, sum3 = 0;

for (int i = h1_temp.Length - 1; i >= 0; i--)
{
st1.Push(Convert.ToInt32(h1_temp[i]));
sum1 += Convert.ToInt32(h1_temp[i]);
}

for (int i = h2_temp.Length - 1; i >= 0; i--)
{
st2.Push(Convert.ToInt32(h2_temp[i]));
sum2 += Convert.ToInt32(h2_temp[i]);
}

for (int i = h3_temp.Length - 1; i >= 0; i--)
{
st3.Push(Convert.ToInt32(h3_temp[i]));
sum3 += Convert.ToInt32(h3_temp[i]);
}

while (!(sum1 == sum2 && sum2 == sum3))
{
if (sum1 < sum2 || sum3 < sum2)
{
sum2 -= st2.Pop();
}

if (sum1 < sum3 || sum2 < sum3)
{
sum3 -= st3.Pop();
}

if (sum2 < sum1 && sum3 < sum1)
{
sum1 -= st1.Pop();
}
}

Console.WriteLine(sum1);
}