You are on page 1of 5

Experiment: 2.

1
(Equal Stacks)

Student Name: Rahul Raj UID: 20BCS7051


Branch: CSE Section/Group: 902 B
Semester: 5th Date of Performance: 22.08.2022
Subject Name: Competitive Coding Subject Code: CSP- 314

1. Aim/Overview of the practical:


Equal Stacks

2. Task to be done/ Which logistics used:


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.
eight.

3. Algorithm/Flowchart (For programming based labs):

Step:-1. Firstly, create a three stack and we have also created a three variable which store the sum of the all
the three individual stack.
Step:-2. Find the length of all the three stacks
Step:- 3. Make a loop for all the individual stack and sum all the elements of an stack and push the elements of
a particular stack.
Step:-4. while any of the stack will not empty - pop out an element from the stack have maximum height.
Step:-5. Then return if all the stack have same height or say sum of remaining element in each array will not
become equal.
Step:-6. If I don't use stack here we can make index element for each array and maintain height.
4. Steps for experiment/practical/Code:
int equalStacks(
(vector<int> h1, vector<int> h2,
vector<int> h3)
{
int sum1 = 0;
int sum2 = 0; int
sum3 = 0;

stack<int> st1;
stack<int> st2; stack<int
int>
st3;

for (int i = h1.size() - 1; i >= 0; i--)


{
sum1 = h1[i] + sum1
sum1;
st1.push(sum1);
}
for (int j = h2.size() - 1; j >= 0; j--)
{
sum2 = h2[j] + sum2
sum2;
st2.push(sum2);
}
for (int k = h3.size();
(); k >= 0; k--)
{

sum3 = h3[k] + sum3


sum3;
st3.push(sum3);
}
while (1)
{
if (st1.empty() || st2
st2.empty() || st3.empty())
return 0;
} sum1 =
st1.top(); sum2 =
st2.top(); sum3 =
st3.top();

if (sum1 == sum2 && sum2 == sum3)


return sum1;
else if (sum1 >= sum2 && sum1 >= sum3)
{
st1.pop();
}
else if(sum2 >= sum1 && sum2 >= sum3)
{
st2.pop();
}
else if (sum3 >= sum1 && sum3 >= sum2)
{
st3.pop();
}
}

5. Observations/Discussions/ Complexity Analysis:


Time Complexity O(N)

Space Complexity O(1)


6. Result/Output/Writing Summary:
Learning outcomes (What I have learnt):
1. Use to stack
2. Performed various stack operations.
3. Using infinite running loop until condition is satisfied.
4. Maintaining all required variable for further uses.
5. Understanding all required test cases for successful submission.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):
Sr. No. Parameters Marks Obtained Maximum Marks
1.

2.

3.

You might also like