Advertisement
deyanmalinov

02. Basic Stack Operations - try-catch

May 8th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         String[] three = scan.nextLine().split(" ");
  7.         int nums = Integer.parseInt(three[0]);
  8.         int remove = Integer.parseInt(three[1]);
  9.         int chech = Integer.parseInt(three[2]);
  10.         String[] line = scan.nextLine().split(" ");
  11.         Deque<Integer> stack = new ArrayDeque<>();
  12.         for (int i = 0; i < line.length; i++) {
  13.             stack.push(Integer.parseInt(line[i]));
  14.         }
  15.         for (int i = 0; i < remove; i++) {
  16.             stack.pop();
  17.         }
  18.         boolean cont = stack.contains(chech);
  19.         if (cont){
  20.             System.out.println(cont);
  21.         }else {
  22.  
  23.             int [] arr = {nums - remove};
  24.             for (int i = 0; i < arr.length; i++) {
  25.                 try {
  26.                     arr[i] = stack.poll();
  27.                 }
  28.                 catch(Exception e) {
  29.                 }
  30.  
  31.             }
  32.  
  33.             Arrays.sort(arr);
  34.             System.out.println(arr[0]);
  35.         }
  36.  
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement