Didart

Basic Queue Operations

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