Skip to main content

EQUAL STACK

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, , , and , describing the respective number 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 describing the cylinder heights in stack .
  • The third line contains  space-separated integers describing the cylinder heights in stack .
  • The fourth line contains  space-separated integers describing the cylinder heights in stack .
Constraints
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:
initial stacks
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  and , and then remove the top two cylinders from stack  (shown below).
modified stacks
As a result, the stacks undergo the following change in height:
All three stacks now have . Thus, we print  as our answer.

Comments

Post a Comment

Popular Posts

Dijkstra's algorithm

Djikstra's algorithm   solves the problem of finding the shortest path from a point in a graph (the  source ) to a destination. It turns out that one can find the shortest paths from a given source to  all  points in a graph in the same time, hence this problem is sometimes called the  single-source shortest paths  problem. This problem is related to the spanning tree one. The graph representing all the paths from one vertex to all the others must be a spanning tree - it must include all vertices. There will also be no cycles as a cycle would define more than one path from the selected vertex to at least one other vertex. For a graph, G = (V,E) where V is a set of vertices and E is a set of edges. Dijkstra's algorithm keeps two sets of vertices: S    the set of vertices whose shortest paths from the source have already been determined  and V-    S :   the remaining v...

Balanced Brackets

Balanced Brackets :- A bracket is considered to be any one of the following characters:  ( ,  ) ,  { ,  } ,  [ , or  ] . Two brackets are considered to be a  matched pair  if the an opening bracket (i.e.,  ( ,  [ , or  { ) occurs to the left of a closing bracket (i.e.,  ) ,  ] , or  } )  of the exact same type . There are three types of matched pairs of brackets:  [] ,  {} , and  () . A matching pair of brackets is  not balanced  if the set of brackets it encloses are not matched. For example,  {[(])} is not balanced because the contents in between  {  and  }  are not balanced. The pair of square brackets encloses a single, unbalanced opening bracket,  ( , and the pair of parentheses encloses a single, unbalanced closing square bracket,  ] . By this logic, we say a sequence of brackets is considered to be  balanced  if the follow...

Kruskal’s Minimum Spanning Tree Algorithm & Programme

PROBLEM :                  Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge of the least possible weight that connects any two trees in the forest. It is a greedy algorithm in graph theory as it finds a minimum spanning tree for a connected weighted graph adding increasing cost arcs at each step. To understand Kruskal's algorithm let us consider the following example − Step 1 - Remove all loops and Parallel Edges Remove all loops and parallel edges from the given graph. In case of parallel edges, keep the one which has the least cost associated and ove all others. Step 2 - Arrange all edges in their increasing order of weight The next step is to create a set of edges and weight, and arrange them in an ascending order of weightage (cost). Step 3 - Add the edge which has the least weightage Now we start adding edges to the graph beginning from the one which has the least weight. Th...