• + 0 comments
    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);
        }
    }