Didart

Basic Stack Operations

Dec 29th, 2022
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. package StacksAndQueues1;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Scanner;
  5.  
  6. public class BasicStackOperations {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String[] input = scanner.nextLine().split(" ");
  11.  
  12.         int countToPush = Integer.parseInt(input[0]);
  13.         int countToPop = Integer.parseInt(input[1]);
  14.         int elementToSearch = Integer.parseInt(input[2]);
  15.  
  16.         ArrayDeque<Integer> stack = new ArrayDeque<>();
  17.         String[] numbersToAdd = scanner.nextLine().split(" ");
  18.  
  19.         for (int i = 0; i < countToPush; i++) {
  20.             stack.push(Integer.parseInt(numbersToAdd[i]));
  21.         }
  22.  
  23.         for (int i = 0; i < countToPop; i++) {
  24.             stack.pop();
  25.         }
  26.  
  27.         if (stack.isEmpty()) {
  28.             System.out.println("0");
  29.         } else if (stack.contains(elementToSearch)) {
  30.             System.out.println(true);
  31.         } else {
  32.             int minElement = stack
  33.                     .stream()
  34.                     .mapToInt(e -> e)
  35.                     .min()
  36.                     .getAsInt();
  37.             System.out.println(minElement);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment