Sort by

recency

|

325 Discussions

|

  • + 2 comments
    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            Deque<Integer> deque = new ArrayDeque<>();
            HashMap<Integer,Integer> subarray = new HashMap<>();
    
            int n = in.nextInt();
            int m = in.nextInt();
            int max = 0;
            for (int i = 0; i < n; i++) {
                int num = in.nextInt();
                deque.add(num);
    
    
                Integer xt = subarray.get(num);
                if(xt == null) subarray.put(num, 1);
                else subarray.put(num, ++xt);
    
                if(deque.size() == m){
                    if(subarray.size() > max) max = subarray.size();
                    int val = deque.poll();
                    Integer rem = subarray.get(val);
                    if(rem != null){
                        rem--;
                        if(rem==0) subarray.remove(val);
                        else subarray.put(val, rem);
                    }
                }
            }
    
    
    
            System.out.println(max);
        }
    
  • + 0 comments
    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);
            }
    }
    
  • + 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<Integer>();
            int n = in.nextInt();
            int m = in.nextInt();
            int max = 0;
            Set<Integer> j = new HashSet<>();
            for (int i = 0; i < n; i++) {
                int num = in.nextInt();
                deque.addLast(num);
                j.add(num);
                if(deque.size()==m){
                    int count = j.size();
                    if(max<count)  max=count ;
                    int target = deque.removeFirst();
                    if(!deque.contains(target)) j.remove(target);
                }
            }
            System.out.println(max);
        }
    }
    
  • + 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<>();
            int n = in.nextInt();
            int m = in.nextInt();
            ArrayList<Integer> sizeArr = new ArrayList<>();
    
            for (int i = 0; i < n; i++) {
                int num = in.nextInt();
                deque.add(num);
            }
            //change to list so that it can be easily sublisted
            List<Integer> list = new ArrayList<Integer>(deque);
    
            //sub-queue loop
            for (int i = 0; i <= n-m; i++) {    //farthest index to start subqueue will be "n-m" (to n)
                HashSet<Integer> hs = new HashSet<>(list.subList(i, i+m));
                sizeArr.add(hs.size());
            }
            System.out.println(Collections.max(sizeArr));
        }
    }
    
  • + 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);
        }
    }