Bug: An elusive creature living in a program that makes it incorrect. The activity of "debugging", or removing bugs from a program, ends when people get tired of doing it, not when the bugs are removed.

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.

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:

8 – 3 = 5

9 – 4 = 5

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:

First get the sum of all the cylinders of each stack and hold this sum in separate variables i.e. sum1, sum2, sum3.

Start looping till sum1, sum2 and sum3 are not equal.

check if height of stack1 is higher than other stacks then pop top cylinder and reduce sum1 by that cylinder weight.

check if height of stack2 is higher than other stacks then pop top cylinder and reduce sum2 by that cylinder weight.

check if height of stack3 is higher than other stacks then pop top cylinder and reduce sum3 by that cylinder weight.

Print the sum1. If there is nothing common the it’s value will be 0, otherwise it’ll have common height.

It’s been a long time since I was trying to solve this problem. Whenever I tried I couldn’t solve it. I always miss some boundary conditions or something else. But today finally I solved it and it’s funny because approach is quite simple and I was trying code like building time machine 😛

Method 1: Brute Force approach – use two nested loops and make a pair and compare sum of them with given number. Its time complexity would be O(n^2).

Method 2: In this method we loop over the array only once. For each item of array, subtract this item from given number and if array item doesn’t exist in our list then add it otherwise print this item and subtraction of array item and given number. Its time complexity would be O(n) as we loop over only once and comparison are also less.

We all know how to check whether a number is prime number or not. As per the definition, a Prime number is a number that can be divisible only with 1 and itself. All other numbers are Non-Primes.

So, 1, 2, 5, 7, 11, 13, 19, 23, 29, 31 … are prime numbers, because all of these numbers can only be divided by either 1 or itself.

That’s theory, no lets see how actually we can check. Suppose you have a number n, so as per theory, if we can find any number other than 1 and n, that is perfect divisor then n is not a Prime number, otherwise it is.

If we try to implement above logic then we’ll have to try all the numbers from 2 to n, but wait a second and think one more time, the biggest divisor of a number can be half of the number.

so, in our case we don’t need to check all the numbers, just check till n/2. Because any number greater than n/2 can never be a divisor of n.

Awesome, now we just cut down half of the iterations and it’s really great in case of a very big number, like 1000000007.

But, half of 1000000007 is still a very big number. If you iterate it one at a time you might get timeout exception. So what do we do now?

hmmm….

hmmm…..

We can do one thing and it’ll solve our problem in case of very BIG NUMBERS. Instead of n/e, if you loop till Square root of n, then we are good. The logic behind this is:

If a number n is not a prime, it can be factored into two factors a and b:

n = a*b

If both a and b were greater than the square root of n, a*b would be greater than n. So at least one of those factors must be less than or equal to the square root of n, and to check if n is prime, we only need to test for factors less than or equal to the square root.

So now, we have solution of all the problems, no lets see the actual code: