You are viewing a single comment's thread. Return to all comments →
public static void main(String[] args) { Scanner in = new Scanner(System.in); Deque<Integer> deque = new ArrayDeque<>(); HashMap<Integer, Integer> map = new HashMap<>(); int n = in.nextInt(); int m = in.nextInt(); int mxSubarr = Integer.MIN_VALUE; for (int i = 0; i < n; i++) { int num = in.nextInt(); deque.addLast(num); if(map.containsKey(num)){ map.put(num, map.get(num)+1); }else{ map.put(num, 1); } if(i+1 >= m){ mxSubarr = Integer.max(mxSubarr, map.keySet().size()); int polled = deque.removeFirst(); if(map.get(polled) == 1){ map.remove(polled); }else{ map.put(polled, map.get(polled) - 1); } } } System.out.println(mxSubarr); }
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 →