{"record":{"id":"3b481e08374db3d6","repo":"TheAlgorithms/Java","slug":"invalid-input-arrays-must-be-non-empty-and-capaci","errorCode":null,"errorMessage":"Invalid input: arrays must be non-empty and capacity/n non-negative.","messagePattern":"Invalid input: arrays must be non-empty and capacity/n non-negative\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOne.java","lineNumber":38,"sourceCode":"     * Solves the 0/1 Knapsack problem using recursion.\n     *\n     * @param values   the array containing values of the items\n     * @param weights  the array containing weights of the items\n     * @param capacity the total capacity of the knapsack\n     * @param n        the number of items\n     * @return the maximum total value achievable within the given weight limit\n     * @throws IllegalArgumentException if input arrays are null, empty, or\n     *     lengths mismatch\n     */\n    public static int compute(final int[] values, final int[] weights, final int capacity, final int n) {\n        if (values == null || weights == null) {\n            throw new IllegalArgumentException(\"Input arrays cannot be null.\");\n        }\n        if (values.length != weights.length) {\n            throw new IllegalArgumentException(\"Value and weight arrays must be of the same length.\");\n        }\n        if (capacity < 0 || n < 0) {\n            throw new IllegalArgumentException(\"Invalid input: arrays must be non-empty and capacity/n \"\n                + \"non-negative.\");\n        }\n        if (n == 0 || capacity == 0 || values.length == 0) {\n            return 0;\n        }\n\n        if (weights[n - 1] <= capacity) {\n            final int include = values[n - 1] + compute(values, weights, capacity - weights[n - 1], n - 1);\n            final int exclude = compute(values, weights, capacity, n - 1);\n            return Math.max(include, exclude);\n        } else {\n            return compute(values, weights, capacity, n - 1);\n        }\n    }\n}\n","sourceCodeStart":20,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOne.java#L20-L54","documentation":"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.","triggerScenarios":"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.","commonSituations":"Passing n = items.length - skipCount where skipCount > items.length; capacity derived from subtraction that underflows; uninitialized int defaulting behavior in edge cases.","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."],"exampleFix":"// before\nint r = KnapsackZeroOne.compute(values, weights, cap - overflow, items - skip); // throws\n\n// after\nint safeCap = Math.max(0, cap - overflow);\nint safeN = Math.max(0, items - skip);\nint r = KnapsackZeroOne.compute(values, weights, safeCap, safeN);","handlingStrategy":"validation","validationCode":"if (capacity < 0 || n < 0) {\n    throw new IllegalArgumentException(\"Capacity and n must be non-negative\");\n}\nint r = KnapsackZeroOne.compute(values, weights, capacity, n);","typeGuard":"static boolean areNonNegative(int capacity, int n) {\n    return capacity >= 0 && n >= 0;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["knapsack","negative-input","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}