TheAlgorithms/Java · error · IllegalArgumentException
Invalid input
Error message
Invalid input
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/slidingwindow/CountDistinctElementsInWindow.java:26
*
* @see <a href="https://www.geeksforgeeks.org/count-distinct-elements-in-every-window-of-size-k/">Reference</a>
*/
public final class CountDistinctElementsInWindow {
private CountDistinctElementsInWindow() {
}
/**
* Returns an array where each element is the count of distinct
* elements in the corresponding window of size k.
*
* @param arr the input array
* @param k the window size
* @return array of distinct element counts per window
*/
public static int[] countDistinct(int[] arr, int k) {
if (arr == null || arr.length == 0 || k <= 0 || k > arr.length) {
throw new IllegalArgumentException("Invalid input");
}
int n = arr.length;
int[] result = new int[n - k + 1];
Map<Integer, Integer> freqMap = new HashMap<>();
for (int i = 0; i < k; i++) {
freqMap.merge(arr[i], 1, Integer::sum);
}
result[0] = freqMap.size();
for (int i = k; i < n; i++) {
freqMap.merge(arr[i], 1, Integer::sum);
int outgoing = arr[i - k];
Integer count = freqMap.get(outgoing);
if (count != null) {View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
int[] counts = CountDistinctElementsInWindow.countDistinct(arr, k);
// after
if (arr == null || arr.length == 0) return new int[0];
if (k < 1 || k > arr.length) throw new IllegalArgumentException("k out of range: " + k);
int[] counts = CountDistinctElementsInWindow.countDistinct(arr, k); Defensive patterns
Strategy: validation
Validate before calling
if (arr == null || arr.length == 0) return new int[0];
if (k < 1 || k > arr.length) throw new IllegalArgumentException("k must be in [1, arr.length]"); Type guard
public static boolean isValidWindow(int[] arr, int k) { return arr != null && arr.length > 0 && k >= 1 && k <= arr.length; } Prevention
- Validate all four conditions separately at your boundary so messages are specific.
- Clamp config-derived k to [1, arr.length].
- Default k to a sensible fraction of array size rather than 0.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- IPv4 address is empty.
- Input cannot be null or empty
- Input cannot be null or empty
- Input cannot be null
- Unit cannot be null.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/debb5d1714962f0e.
Report an issue: GitHub.