We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<>();
Map<Integer, Integer> map = new HashMap<>();
int n = in.nextInt();
int m = in.nextInt();
int res = 0;
for (int i = 0; i < n; i++) {
int num = in.nextInt();
deque.add(num);
map.put(num, map.getOrDefault(num, 0)+1);
if(deque.size()==m){
res = Math.max(res, map.size());
int frontElement = deque.poll();
map.put(frontElement, map.get(frontElement)-1);
if(map.get(frontElement)==0){
map.remove(frontElement);
}
}
}
System.out.println(res);
}
}
Cookie support is required to access HackerRank
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 →