Experiment-3
Aim:- 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
h1=[1,2,1,1]
h2=[1,1,2]
h3=[1,1]
There are 4,3 and 2 cylinders in the three stacks, with their heights in the three arrays. Remove the top 2
cylinders from h1 (heights = [1, 2]) and from h2 (heights = [1, 1]) so that the three stacks all are 2 units
tall. Return 2 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, n1, n2, and n3, 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 n1 space-separated integers, the cylinder heights in stack 1. The first element
is the top cylinder of the stack.
The third line contains n2 space-separated integers, the cylinder heights in stack 2. The first element is
the top cylinder of the stack.
The fourth line contains n3 space-separated integers, the cylinder heights in stack 3. The first element is
the top cylinder of the stack.
Constraints
0<n1,n2,n3<=10^5
0<height of any cylinder<=100
Sample Input
STDIN Function
----- --------
534 h1[] size n1 = 5, h2[] size n2 = 3, h3[] size n3 = 4
3 2 1 1 1 h1 = [3, 2, 1, 1, 1]
432 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 1, 2 and 3, and then remove the top two
cylinders from stack 3 (shown below).
The stack heights are reduced as follows:
1. 8-3=5
2. 9-4=5
3. 7-1-1=5
All three stacks now have height=5 , the value to return.
Code:-
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int equalStacks(vector<int>& h1, vector<int>& h2, vector<int>& h3) {
// Reverse the stacks to simplify popping from the top
reverse(h1.begin(), h1.end());
reverse(h2.begin(), h2.end());
reverse(h3.begin(), h3.end());
// Calculate cumulative heights for each stack
partial_sum(h1.begin(), h1.end(), h1.begin());
partial_sum(h2.begin(), h2.end(), h2.begin());
partial_sum(h3.begin(), h3.end(), h3.begin());
// Find the common height by iterating until a common height is found or one of the stacks is empty
while (!h1.empty() && !h2.empty() && !h3.empty() &&
!(h1.back() == h2.back() && h2.back() == h3.back())) {
if (h1.back() >= h2.back() && h1.back() >= h3.back()) {
h1.pop_back();
} else if (h2.back() >= h1.back() && h2.back() >= h3.back()) {
h2.pop_back();
} else {
h3.pop_back();
// Return the common height if found, otherwise return 0
return (h1.empty() || h2.empty() || h3.empty()) ? 0 : h1.back();
int main() {
int n1, n2, n3;
cin >> n1 >> n2 >> n3;
vector<int> h1(n1);
vector<int> h2(n2);
vector<int> h3(n3);
for (int i = 0; i < n1; i++) {
cin >> h1[i];
for (int i = 0; i < n2; i++) {
cin >> h2[i];
for (int i = 0; i < n3; i++) {
cin >> h3[i];
}
int result = equalStacks(h1, h2, h3);
cout << "Maximum possible common height: " << result << endl;
return 0;
Input:
Output: