Advertisement
IrinaIgnatova

Basic Stack Operations

Sep 27th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         int[] tokensValue = Arrays.stream(scanner.nextLine().split(" "))
  13.                 .mapToInt(Integer::parseInt)
  14.                 .toArray();
  15.         ArrayDeque<Integer> stack = new ArrayDeque<>();
  16.  
  17.         Arrays.stream(scanner.nextLine().split(" "))
  18.                 .mapToInt(Integer::parseInt)
  19.                 .limit(tokensValue[0])
  20.                 .forEach(stack::push);
  21.  
  22.         for (int i = 0; i < tokensValue[1]; i++) {
  23.             stack.pop();
  24.         }
  25.  
  26.         int minValue = Integer.MAX_VALUE;
  27.         boolean hasRequiredValue = false;
  28.         if (stack.isEmpty()) {
  29.             minValue = 0;
  30.         }
  31.  
  32.         while (!stack.isEmpty() && !hasRequiredValue) {
  33.             int number = stack.pop();
  34.  
  35.             if (number == tokensValue[2]) {
  36.                 hasRequiredValue = true;
  37.             }
  38.             if (number < minValue) {
  39.                 minValue = number;
  40.             }
  41.         }
  42.         //Print Section
  43.  
  44.  
  45.         if (hasRequiredValue) {
  46.             System.out.println("true");
  47.         } else {
  48.             System.out.println(minValue);
  49.         }
  50.  
  51.  
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement