You are on page 1of 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


Experiment-1.2
Student Name: Dhruv UID: 21BCS3484
Branch: CSE Section/Group:802-B
Semester: 5th Date of Performance:15-08-2023
Subject Name: AP Lab Subject Code:21CSP-314

Aim:- WAP to equal the height of array using Stack.


Objective:- The Objective of this experiment is to find the maximum possible
height of the stacks such that all of the stacks are exactly the same height.
Code:-
#include <iostream>
#include <vector>
using namespace std;
int equalStacks(vector<int> h1, vector<int> h2, vector<int> h3)
{
int n1 = h1.size(), n2 = h2.size(), n3 = h3.size();
int sum1 = 0, sum2 = 0, sum3 = 0; for (int i = 0; i < n1; ++i) sum1 += h1[i];
for (int i = 0; i < n2; ++i) sum2 += h2[i];
for (int i = 0; i < n3; ++i) sum3 += h3[i];
int idx1 = 0, idx2 = 0, idx3 = 0;
while (!(sum1 == sum2 && sum2 == sum3))
{
int maxSum = max(max(sum1, sum2), sum3);
if (sum1 == maxSum) sum1 -= h1[idx1++];
if (sum2 == maxSum) sum2 -= h2[idx2++];
if (sum3 == maxSum) sum3 -= h3[idx3++];
}
return sum1;
}
int main()
{
int n1, n2, n3;
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


cin >> n1 >> n2 >> n3;
vector<int> h1(n1), h2(n2), 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 << result << endl;
return 0;
}

Output:-
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


Aim:- WAP to Determine the minimum number of moves required to reduce the
value N of to 0.
Objective:- The objective of this experiment is to determine the number of moves
required to reduce the value of N to 0.
Code:-
#include <iostream>
#include <vector>
using namespace std;
int twoStacks(int maxSum, vector<int> a, vector<int> b)
{ int n = a.size();
int m = b.size();
int i = 0, j = 0;
int totalSum = 0, maxScore = 0;
while (j < m && totalSum + b[j] <= maxSum)
{ totalSum += b[j];
j++;
}
maxScore = j;
for (i = 0; i < n && j >= 0; i++)
{
totalSum += a[i];
while (totalSum > maxSum && j > 0)
{
j--;
totalSum -= b[j];
}
if (totalSum <= maxSum) maxScore = max(maxScore, i
+j
+ 1);
}
return maxScore;
}
int main()
{
int g; cin >> g;
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


while (g--)
{ int n, m, maxSum;
cin >> n >> m >> maxSum;
vector<int> a(n);
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
vector<int> b(m);
for (int i = 0; i < m; ++i)
{ cin >> b[i];
}
int result = twoStacks(maxSum, a, b);
cout << result << endl;
}
return 0;
}
Output:-

You might also like