You are viewing a single comment's thread. Return to all comments →
import java.util.*;
public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
int numberElements = scanner.nextInt(); // Declare and initialize int numberSubgroups = scanner.nextInt(); // Declare and initialize Deque<Integer> elementsWindow = new ArrayDeque<>(numberSubgroups); // Declare and initialize int maxUniqueValues = 1; HashMap<Integer, Integer> frequency = new HashMap<>(numberElements); // Declare and initialize for (int i = 0; i < numberElements; i++) { int element = scanner.nextInt(); elementsWindow.addLast(element); if (!frequency.containsKey(element)) { frequency.put(element, 1); } else { frequency.put(element, frequency.get(element) + 1); } if (elementsWindow.size() == numberSubgroups) { int numInFrequency = 0; for (int value : frequency.values()) { if (value > 0) { numInFrequency++; } } maxUniqueValues = Math.max(maxUniqueValues, numInFrequency); Integer elementRemoved = elementsWindow.removeFirst(); int updatedCount = frequency.get(elementRemoved) - 1; if (updatedCount == 0) { frequency.remove(elementRemoved); } else { frequency.put(elementRemoved, updatedCount); } } } System.out.println(maxUniqueValues); scanner.close(); }
}
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 →
import java.util.*;
public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
}