Advertisement
deyanmalinov

2. Basic Stack Operations

Mar 24th, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         String[] comands = scan.nextLine().split("\\s+");
  9.         String[] line = scan.nextLine().split(" ");
  10.         ArrayDeque<Integer> stack = new ArrayDeque<>();
  11.         for (int i = 0; i < line.length; i++) {
  12.             stack.push(Integer.parseInt(line[i]));
  13.         }
  14.         int times= Integer.parseInt(comands[1]);
  15.         for (int i = 0; i <times ; i++) {
  16.             stack.pop();
  17.         }
  18.         if (stack.isEmpty()){
  19.             System.out.println(0);
  20.         }else {
  21.             int element = Integer.parseInt(comands[2]);
  22.             if (stack.contains(element)) {
  23.                 System.out.println("true");
  24.             } else {
  25.                 System.out.println(Collections.min(stack));
  26.             }
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement