TheAlgorithms/Java · error · IllegalArgumentException
Invalid input: arrays must be non-empty and capacity/n non-n
Error message
Invalid input: arrays must be non-empty and capacity/n non-negative.
What it means
KnapsackZeroOne.compute requires capacity >= 0 and n >= 0. Negative capacity has no physical meaning, and negative n would produce invalid array indexing (n-1 < 0). The method throws IllegalArgumentException when either is negative. Note the message also references non-empty arrays, though the actual check is purely on capacity and n being non-negative.
Source
Thrown at src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOne.java:38
* Solves the 0/1 Knapsack problem using recursion.
*
* @param values the array containing values of the items
* @param weights the array containing weights of the items
* @param capacity the total capacity of the knapsack
* @param n the number of items
* @return the maximum total value achievable within the given weight limit
* @throws IllegalArgumentException if input arrays are null, empty, or
* lengths mismatch
*/
public static int compute(final int[] values, final int[] weights, final int capacity, final int n) {
if (values == null || weights == null) {
throw new IllegalArgumentException("Input arrays cannot be null.");
}
if (values.length != weights.length) {
throw new IllegalArgumentException("Value and weight arrays must be of the same length.");
}
if (capacity < 0 || n < 0) {
throw new IllegalArgumentException("Invalid input: arrays must be non-empty and capacity/n "
+ "non-negative.");
}
if (n == 0 || capacity == 0 || values.length == 0) {
return 0;
}
if (weights[n - 1] <= capacity) {
final int include = values[n - 1] + compute(values, weights, capacity - weights[n - 1], n - 1);
final int exclude = compute(values, weights, capacity, n - 1);
return Math.max(include, exclude);
} else {
return compute(values, weights, capacity, n - 1);
}
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Clamp capacity and n to zero with Math.max(0, ...) before the call.
- Validate the expressions producing capacity and n for underflow on edge-case data.
- Handle the n == 0 or capacity == 0 early-return case explicitly before calling compute.
Example fix
// before int r = KnapsackZeroOne.compute(values, weights, cap - overflow, items - skip); // throws // after int safeCap = Math.max(0, cap - overflow); int safeN = Math.max(0, items - skip); int r = KnapsackZeroOne.compute(values, weights, safeCap, safeN);
Defensive patterns
Strategy: validation
Validate before calling
if (capacity < 0 || n < 0) {
throw new IllegalArgumentException("Capacity and n must be non-negative");
}
int r = KnapsackZeroOne.compute(values, weights, capacity, n); Type guard
static boolean areNonNegative(int capacity, int n) {
return capacity >= 0 && n >= 0;
} Prevention
- Clamp capacity and n with Math.max(0, expr) when they come from subtraction.
- Handle the n==0 or capacity==0 early-return case before calling compute.
When it happens
Trigger: Calling compute with capacity < 0 or n < 0. This can happen when n is computed as values.length - offset where offset exceeds the length, or when capacity is decremented past zero in a loop.
Common situations: Passing n = items.length - skipCount where skipCount > items.length; capacity derived from subtraction that underflows; uninitialized int defaulting behavior in edge cases.
Related errors
- Input n must be non-negative
- Weight capacity should not be negative.
- Weights and values must be non-null and of the same length.
- Weights must be positive.
- Input arrays cannot be null.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/3b481e08374db3d6.
Report an issue: GitHub.