{"record":{"id":"debb5d1714962f0e","repo":"TheAlgorithms/Java","slug":"invalid-input-debb5d","errorCode":null,"errorMessage":"Invalid input","messagePattern":"Invalid input","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java","lineNumber":26,"sourceCode":" *\n * @see <a href=\"https://www.geeksforgeeks.org/count-distinct-elements-in-every-window-of-size-k/\">Reference</a>\n */\npublic final class CountDistinctElementsInWindow {\n\n    private CountDistinctElementsInWindow() {\n    }\n\n    /**\n     * Returns an array where each element is the count of distinct\n     * elements in the corresponding window of size k.\n     *\n     * @param arr the input array\n     * @param k   the window size\n     * @return array of distinct element counts per window\n     */\n    public static int[] countDistinct(int[] arr, int k) {\n        if (arr == null || arr.length == 0 || k <= 0 || k > arr.length) {\n            throw new IllegalArgumentException(\"Invalid input\");\n        }\n\n        int n = arr.length;\n        int[] result = new int[n - k + 1];\n        Map<Integer, Integer> freqMap = new HashMap<>();\n\n        for (int i = 0; i < k; i++) {\n            freqMap.merge(arr[i], 1, Integer::sum);\n        }\n        result[0] = freqMap.size();\n\n        for (int i = k; i < n; i++) {\n            freqMap.merge(arr[i], 1, Integer::sum);\n\n            int outgoing = arr[i - k];\n\n            Integer count = freqMap.get(outgoing);\n            if (count != null) {","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java#L8-L44","documentation":"Thrown by CountDistinctElementsInWindow.countDistinct(int[], int) when arr is null, empty, k <= 0, or k > arr.length. A single guard collapses four distinct precondition failures into one message because all of them make the sliding-window count undefined. The method needs at least one valid window to produce output.","triggerScenarios":"Calling countDistinct(null, k); countDistinct(new int[0], k); countDistinct(arr, 0); countDistinct(arr, -1); countDistinct(arr, arr.length + 1) where the window is larger than the array.","commonSituations":"Window size k read from config and left at 0/default; array loaded from a stream that produced no elements; k derived from a fraction that rounds to 0 for small arrays.","solutions":["Validate arr != null && arr.length > 0 && k >= 1 && k <= arr.length before calling.","When computing k from a ratio (e.g. size * p), clamp to at least 1 and at most arr.length.","Document the single-message guard at your API boundary so callers know which condition failed via separate checks."],"exampleFix":"// before\nint[] counts = CountDistinctElementsInWindow.countDistinct(arr, k);\n\n// after\nif (arr == null || arr.length == 0) return new int[0];\nif (k < 1 || k > arr.length) throw new IllegalArgumentException(\"k out of range: \" + k);\nint[] counts = CountDistinctElementsInWindow.countDistinct(arr, k);","handlingStrategy":"validation","validationCode":"if (arr == null || arr.length == 0) return new int[0];\nif (k < 1 || k > arr.length) throw new IllegalArgumentException(\"k must be in [1, arr.length]\");","typeGuard":"public static boolean isValidWindow(int[] arr, int k) { return arr != null && arr.length > 0 && k >= 1 && k <= arr.length; }","tryCatchPattern":null,"preventionTips":["Validate all four conditions separately at your boundary so messages are specific.","Clamp config-derived k to [1, arr.length].\n","Default k to a sensible fraction of array size rather than 0."],"tags":["sliding-window","input-validation","config","null-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}