You are viewing a single comment's thread. Return to all comments →
I solved this issue using Java 15, also works with Java 17:
public static void main(String[] args) { final var scanner = new Scanner(System.in); final var queue = new ArrayDeque<Integer>(); final var set = new HashSet<Integer>(); final var n = scanner.nextInt(); final var m = scanner.nextInt(); final var output = IntStream.rangeClosed(1, n) .mapToLong(i -> { final var input = scanner.nextInt(); var max = Integer.MIN_VALUE; queue.add(input); set.add(input); if (queue.size() == m) { max = set.size(); final var first = (int) queue.remove(); if (!queue.contains(first)) set.remove(first); } return max; }) .max() .orElse(0); System.out.println(output); }
Seems like cookies are disabled on this browser, please enable them to open this website
Java Dequeue
You are viewing a single comment's thread. Return to all comments →
I solved this issue using Java 15, also works with Java 17: