0% found this document useful (0 votes)
39 views6 pages

Experiment 1.2

1. The document describes an experiment conducted by Priyanshi Karol to implement stacks and queues in HackerRank. 2. Two problems are discussed - Equal Stacks which involves balancing the heights of three stacks, and Game of Stacks which involves removing integers from two stacks without exceeding a maximum sum. 3. Pseudocode and analysis is provided for both problems solving them in optimal time complexity.

Uploaded by

Status king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views6 pages

Experiment 1.2

1. The document describes an experiment conducted by Priyanshi Karol to implement stacks and queues in HackerRank. 2. Two problems are discussed - Equal Stacks which involves balancing the heights of three stacks, and Game of Stacks which involves removing integers from two stacks without exceeding a maximum sum. 3. Pseudocode and analysis is provided for both problems solving them in optimal time complexity.

Uploaded by

Status king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment 1.

Name: Priyanshi Karol UID: 21BCS3089


Branch: BE-CSE Section/Group: IoT-627-B
Semester: 5th Date of Performance: 20/08/23
Subject: Advance Programming Lab Subject Code: 21CSP-314

AIM: Implement the Stacks and Queues in Hacker rank.

Q1) EQUAL STACKS


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.

CODE:
public class Solution {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

int n1 = scanner.nextInt();
int n2 = scanner.nextInt();
int n3 = scanner.nextInt();

int[] h1 = new int[n1];


int[] h2 = new int[n2];
int[] h3 = new int[n3];

for (int i = 0; i < n1; i++) {


h1[i] = scanner.nextInt();
}

for (int i = 0; i < n2; i++) {


h2[i] = scanner.nextInt();
}
for (int i = 0; i < n3; i++) {
h3[i] = scanner.nextInt();
}

System.out.println(equalStacks(h1, h2, h3));

scanner.close();
}

public static int equalStacks(int[] h1, int[] h2, int[] h3) {


int sum1 = 0, sum2 = 0, sum3 = 0;

for (int height : h1) sum1 += height;


for (int height : h2) sum2 += height;
for (int height : h3) sum3 += height;

int inx1 = 0, idx2 = 0, idx3 = 0;

while (true) {
if (sum1 == sum2 && sum2 == sum3) {
return sum1;
}

int res = Math.max(sum1, Math.max(sum2, sum3));

if (res == sum1) {
sum1 -= h1[inx1];
inx1++;
} else if (res == sum2) {
sum2 -= h2[idx2];
idx2++;
} else if (res == sum3) {
sum3 -= h3[idx3];
idx3++;
}
}
}
}
OUTPUT:
QUES 2) GAME OF STACKS:
Alexa has two stacks of non-negative integers, stack a[n] and stack b[m] where index 0 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 a or stack b.
• 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 maxSum 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 a, b, maxSum and for g games, find the maximum possible score Nick can achieve.

CODE:

import java.util.Scanner;

public class TwoStacks{


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int g = scanner.nextInt(); // Number of games

for (int i = 0; i < g; i++) {


int n = scanner.nextInt();
int m = scanner.nextInt();
int maxSum = scanner.nextInt();

int[] a = new int[n];


for (int j = 0; j < n; j++) {
a[j] = scanner.nextInt();
}

int[] b = new int[m];


for (int j = 0; j < m; j++) {
b[j] = scanner.nextInt();
}

int result = func(maxSum, a, b);


System.out.println(result);
}
scanner.close();
}

static int func(int maxSum, int[] a, int[] b) {


int n = a.length;
int m = b.length;

int finalScore = 0;
int sum = 0;
int i = 0;
int j = 0;

// Add elements from the first stack to the sum until maxSum is exceeded
while (i < n && sum + a[i] <= maxSum) {
sum += a[i];
i++;
}
finalScore = i; // Count of elements from the first stack

// Add elements from the second stack while removing elements from the first stack
while (j < m && i >= 0) {
sum += b[j];
j++;

// Remove elements from the first stack as long as the sum exceeds maxSum
while (i > 0 && sum > maxSum) {
i--;
sum -= a[i];
}

if (sum <= maxSum) {


finalScore = Math.max(finalScore, i + j); // Total selections
}
}

return finalScore;
}
}
OUTPUT:

COMPLEXITY:
The time complexity for the QUEUE is O(N2).
The time complexity for the STACK is O(1), except for the search operation which has a time
complexity of O(n).

You might also like