{"record":{"id":"a357dec972344393","repo":"TheAlgorithms/Java","slug":"capacity-must-not-be-negative","errorCode":null,"errorMessage":"Capacity must not be negative.","messagePattern":"Capacity must not be negative\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOneTabulation.java","lineNumber":44,"sourceCode":"\n    /**\n     * Solves the 0-1 Knapsack problem using the bottom-up tabulation technique.\n     * @param values the values of the items\n     * @param weights the weights of the items\n     * @param capacity the total capacity of the knapsack\n     * @param itemCount the number of items\n     * @return the maximum value that can be put in the knapsack\n     * @throws IllegalArgumentException if input arrays are null, of different lengths,or if capacity or itemCount is invalid\n     */\n    public static int compute(final int[] values, final int[] weights, final int capacity, final int itemCount) {\n        if (values == null || weights == null) {\n            throw new IllegalArgumentException(\"Values and weights arrays must not be null.\");\n        }\n        if (values.length != weights.length) {\n            throw new IllegalArgumentException(\"Values and weights arrays must be non-null and of same length.\");\n        }\n        if (capacity < 0) {\n            throw new IllegalArgumentException(\"Capacity must not be negative.\");\n        }\n        if (itemCount < 0 || itemCount > values.length) {\n            throw new IllegalArgumentException(\"Item count must be between 0 and the length of the values array.\");\n        }\n\n        final int[][] dp = new int[itemCount + 1][capacity + 1];\n\n        for (int i = 1; i <= itemCount; i++) {\n            final int currentValue = values[i - 1];\n            final int currentWeight = weights[i - 1];\n\n            for (int w = 1; w <= capacity; w++) {\n                if (currentWeight <= w) {\n                    final int includeItem = currentValue + dp[i - 1][w - currentWeight];\n                    final int excludeItem = dp[i - 1][w];\n                    dp[i][w] = Math.max(includeItem, excludeItem);\n                } else {\n                    dp[i][w] = dp[i - 1][w];","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOneTabulation.java#L26-L62","documentation":"KnapsackZeroOneTabulation.compute allocates dp[itemCount + 1][capacity + 1]. A negative capacity would cause a NegativeArraySizeException during table allocation, so the method rejects capacity < 0 with IllegalArgumentException upfront.","triggerScenarios":"Calling compute with capacity < 0 — from subtraction that underflows, an uninitialized -1 sentinel, or user input parsed without clamping.","commonSituations":"Using -1 as a 'not yet set' sentinel for capacity; capacity = budget - cost where cost > budget; deserializing a negative capacity from malformed config.","solutions":["Clamp capacity to zero: Math.max(0, capacity).","Replace any -1 sentinel with the real value before the call.","Validate capacity >= 0 at the data source."],"exampleFix":"// before\nint r = KnapsackZeroOneTabulation.compute(values, weights, budget - cost, count); // throws\n\n// after\nint cap = Math.max(0, budget - cost);\nint r = KnapsackZeroOneTabulation.compute(values, weights, cap, count);","handlingStrategy":"validation","validationCode":"if (capacity < 0) {\n    throw new IllegalArgumentException(\"Capacity must be >= 0\");\n}\nint r = KnapsackZeroOneTabulation.compute(values, weights, capacity, itemCount);","typeGuard":"static boolean isValidCapacity(int cap) {\n    return cap >= 0;\n}","tryCatchPattern":null,"preventionTips":["Replace -1 sentinels with the actual capacity value.","Clamp capacity with Math.max(0, capacity) when it comes from subtraction."],"tags":["knapsack","negative-capacity","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}