Mistakes
Mistakes
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Program2b{
q2.add(x);
while (!q1.isEmpty()) {
q2.add(q1.remove());
q1 = q2;
q2 = temp;
if (q1.isEmpty()) {
System.out.println("Stack is empty!");
return -1;
return q1.remove();
if (q1.isEmpty()) {
System.out.println("Stack is empty!");
return -1;
return q1.peek();
return q1.isEmpty();
if (q1.isEmpty()) {
System.out.println("Stack is empty!");
return;
do {
choice = sc.nextInt();
switch (choice) {
case 1:
value = sc.nextInt();
stack.push(value);
break;
case 2:
if (popped != -1)
break;
case 3:
if (top != -1)
break;
case 4:
stack.display();
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice.");
sc.close();
6a)
import java.util.HashMap;
import java.util.Scanner;
public class Program6a {
if (map.containsKey(complement)) {
map.put(nums[i], i);
int n = sc.nextInt();
nums[i] = sc.nextInt();
if (result.length == 0) {
} else {
System.out.println("Indices of the two numbers: [" + result[0] + ", " + result[1] + "]");
sc.close();
} }
Set -13a)
import java.util.*;
Arrays.sort(greed);
Arrays.sort(cookies);
int i = 0, j = 0;
i++;
j++;
return i;
int n = sc.nextInt();
int[] greed = new int[n];
greed[i] = sc.nextInt();
int m = sc.nextInt();
cookies[i] = sc.nextInt();
sc.close();
Set14
14a)
import java.util.*;
while (true) {
System.out.print("Enter payment amount of the customer (or type 'exit' to quit): ");
if (input.equalsIgnoreCase("exit")) break;
if (payment == 5) {
five++;
if (five >= 1) {
five--;
ten++;
} else {
break;
ten--;
five--;
} else {
break;
sc.close();
Set 17
17a)
import java.util.Scanner;
class Node {
int data;
Node(int item) {
data = item;
if (root == null) {
return 0;
currentSum += root.data;
return 1;
else
return root;
int n = sc.nextInt();
System.out.println("Enter " + n + " node values:");
import java.util.Scanner;
long result = 1;
result *= i;
return result;
int m = sc.nextInt();
Enter n (rows): 2
Enter m (columns): 2
Set20
20a)
import java.util.Arrays;
import java.util.Scanner;
int n = scanner.nextInt();
arr[i] = scanner.nextInt();
findTriplet(arr);
}
int n = arr.length;
for (int i = 0; i < n - 1; i++) { // Loop through each element as the third element
System.out.println("Triplet found: " + arr[left] + ", " + arr[right] + ", " + arr[i]);
found = true;
left++; // Move left pointer to the right if sum is less than arr[i]
} else {
right--; // Move right pointer to the left if sum is greater than arr[i]
if (!found) {
System.out.println("No triplet found where the sum of two elements equals the third
element.");
Brute forcemethod
import java.util.Scanner;
int n = scanner.nextInt();
arr[i] = scanner.nextInt();
checkTriplet(arr);
found = true;
if (!found) {
Set 40 a
import java.util.Scanner;
return 0;
}
int cash = 0; // Maximum profit when not holding a stock
// Update hold: Either we don't buy or we buy after selling previously held stock
return cash; // The maximum profit is when we are not holding any stock
int n = scanner.nextInt();
prices[i] = scanner.nextInt();
Another method
import java.util.Scanner;
import java.util.Locale;
String stockSymbol;
int quantity=0;
while (true) {
System.out.println("\nOptions:");
System.out.println("3. Exit");
switch (choice) {
case 1:
if (hasStock) {
break;
stockSymbol = scanner.nextLine();
quantity = scanner.nextInt();
purchasePrice = scanner.nextDouble();
hasStock = true;
break;
case 2:
if (!hasStock) {
break;
break;
// Calculate profit/loss
System.out.println("Sold " + quantity + " shares of the stock for $" + sellingPrice + " per
share.");
hasStock = false;
break;
case 3:
scanner.close();
return;
default: