Sort by

recency

|

326 Discussions

|

  • + 0 comments

    My version:

     public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int numberElements = scanner.nextInt();
        int numberSubgroups = scanner.nextInt();
        Deque<Integer> elementsWindow = new ArrayDeque<>(numberSubgroups);
        int maxUniqueValues = 1;
        HashMap<Integer, Integer> frecuencia = new HashMap<>(numberElements);
    
        for (int i = 0; i < numberElements; i++) {
            int element = scanner.nextInt();
            elementsWindow.addLast(element);
            frecuencia.put(element, frecuencia.getOrDefault(element, 0) + 1);
    
            if (elementsWindow.size() == numberSubgroups) {
                int numInFrecuencia = frecuencia.size();
    
                maxUniqueValues = Math.max(maxUniqueValues, numInFrecuencia);
                Integer elementRemoved = elementsWindow.removeFirst();
                int updatedCount = frecuencia.get(elementRemoved) - 1;
    
                if (updatedCount == 0) {
                    frecuencia.remove(elementRemoved);
                } else {
                    frecuencia.put(elementRemoved, updatedCount);
                }
            }
    
        }
        System.out.println(maxUniqueValues);
    }
    
  • + 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));
        }
    }