Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

To demonstrate the concept of Stack and Queue., Lab Reports of Computer Science

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.

Typology: Lab Reports

2021/2022

Uploaded on 11/04/2022

diksha-sharma-25
diksha-sharma-25 🇮🇳

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Experiment 2
Student Name: Diksha Sharma UID: 20BCS1458
Branch: Computer Science Engineering Section/Group: 20BCS_MM-807-A
Semester: 5 Date of Performance:23/08/2022
Subject Name: Competitive Coding Subject Code: 20CSP-314
Problem 1:
https://www.hackerrank.com/challenges/equal-stacks/problem
1. Aim/Overview of the practical:
To demonstrate the concept of Stack and Queue.
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.
3. Algorithm/Flowchart:
1) First we’ll make three vectors and take their input.
2) Then we’ll reverse all the arrays.
3) Then we’ll find the sum of the first array and in every step turn on index in the
ok array to 1
4) Repeat the same process for second array and if the index of the ok array is
already 1, the increment it to 2.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download To demonstrate the concept of Stack and Queue. and more Lab Reports Computer Science in PDF only on Docsity!

Experiment 2

Student Name: Diksha Sharma UID: 20BCS Branch: Computer Science Engineering Section/Group: 20BCS_MM-807-A Semester: 5 Date of Performance:23/08/ Subject Name: Competitive Coding Subject Code: 20CSP- Problem 1: https://www.hackerrank.com/challenges/equal-stacks/problem

**1. Aim/Overview of the practical: To demonstrate the concept of Stack and Queue.

  1. 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. 3. Algorithm/Flowchart:
  1. First we’ll make three vectors and take their input.
  2. Then we’ll reverse all the arrays.
  3. Then we’ll find the sum of the first array and in every step turn on index in the ok array to 1
  4. Repeat the same process for second array and if the index of the ok array is already 1, the increment it to 2.
  1. Repeat the same process for 3rd array and if the ok array is 2, store the sum in ret variable and return it as the answer.

4. Code: #include<bits/stdc++.h> using namespace std; int n1, n2, n3; int ok[20000000], a[100010], b[100100], c[100100]; int main() { cin >> n1 >> n2 >> n3; for(int i = 0; i < n1; i++) cin >> a[i]; for(int i = 0; i < n2; i++) cin >> b[i]; for(int i = 0; i < n3; i++) cin >> c[i]; reverse(a, a + n1); reverse(b, b + n2); reverse(c, c + n3); int ret = 0, sum = 0, ai; for(int i = 0; i < n1; i++) { ai = a[i]; sum += ai; ok[sum] = 1; } sum = 0; for(int i = 0; i < n2; i++) { ai = b[i]; sum += ai; if (ok[sum] == 1) ok[sum] = 2; } sum = 0; for(int i = 0; i < n3; i++) { ai = c[i]; sum += ai; if (ok[sum] == 2) ret = sum; } cout << ret << endl; return 0;

Problem 2: https://www.hackerrank.com/challenges/game-of-two-stacks/problem?isFullScreen=true

**1. Aim/Overview of the practical: To demonstrate the concept of Stack and Queue.

  1. Task to be done/ Which logistics used:** Alexa has two stacks of non-negative integers, stack and stack where index denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack or stack. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified from the game if, at any point, his running sum becomes greater than some integer given at the beginning of the game.

Nick's final score is the total number of integers he has removed from the two stacks. Given , , and for games, find the maximum possible score Nick can achieve

3. Algorithm:

  1. Input 2 vectors.
  2. Find and store prefix array of both the vectors.
  3. Iterate through the first array and if a[i]<=x, store the max of ans and (index+ upperbound of the remaining sum in array b).
  4. Repeat the same step for array b;
  5. Return max of all as the answer. 4. Code: #include <bits/stdc++.h> using namespace std; #define int long long int32_t main(){ int t; cin>>t; while(t--){ int n, m, x; cin >> n >> m >> x; vector a(n); for(int i=0; i<n; i++){ cin>>a[i]; } vector b(m); for(int i=0; i<m; i++){ cin>>b[i]; } for(int i=1; i<n; i++){ a[i]+=a[i-1]; } for(int i=1; i<m; i++){ b[i]+=b[i-1];

Learning outcomes (What I have learnt):

  1. Learnt to deal with stacks and queues.
  2. Learnt to use push and pop operations in stack. 3. Learnt about upper_bound() function in stacks. 4. Learnt about max() function in stacks.