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.*;
import java.util.stream.Collectors;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<>();
Map<Integer,Integer> cnt = new HashMap<>();
int n = in.nextInt();
int m = in.nextInt();
int mx = 0;
in.nextLine();
List<Integer> A = Arrays.stream(in.nextLine().trim().split("\\s+"))
.map(Integer::parseInt).collect(Collectors.toList());
for (int i = 0; i < n; i++) {
deque.addLast(A.get(i));
cnt.put(A.get(i),cnt.getOrDefault(A.get(i), 0)+1);
if (deque.size()>m) {
int rm = deque.removeFirst();
cnt.put(rm,cnt.get(rm)-1);
if (cnt.get(rm) == 0) cnt.remove(rm);
}
if (deque.size()== m) mx = Math.max(mx,cnt.size());
}
System.out.println(mx);
}
}
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 →