{"record":{"id":"3e87f5b68678917c","repo":"TheAlgorithms/Java","slug":"rod-length-cannot-be-negative","errorCode":null,"errorMessage":"Rod length cannot be negative.","messagePattern":"Rod length cannot be negative\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java","lineNumber":26,"sourceCode":"    private RodCutting() {\n    }\n\n    /**\n     * This method calculates the maximum obtainable value for cutting a rod of length n\n     * into different pieces, given the prices for each possible piece length.\n     *\n     * @param price An array representing the prices of different pieces, where price[i-1]\n     *              represents the price of a piece of length i.\n     * @param n     The length of the rod to be cut.\n     * @throws IllegalArgumentException if the price array is null or empty, or if n is less than 0.\n     * @return The maximum obtainable value.\n     */\n    public static int cutRod(int[] price, int n) {\n        if (price == null || price.length == 0) {\n            throw new IllegalArgumentException(\"Price array cannot be null or empty.\");\n        }\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Rod length cannot be negative.\");\n        }\n\n        // Create an array to store the maximum obtainable values for each rod length.\n        int[] val = new int[n + 1];\n        val[0] = 0;\n\n        // Calculate the maximum value for each rod length from 1 to n.\n        for (int i = 1; i <= n; i++) {\n            int maxVal = Integer.MIN_VALUE;\n            // Try all possible ways to cut the rod and find the maximum value.\n            for (int j = 1; j <= i; j++) {\n                maxVal = Math.max(maxVal, price[j - 1] + val[i - j]);\n            }\n            // Store the maximum value for the current rod length.\n            val[i] = maxVal;\n        }\n\n        // The final element of 'val' contains the maximum obtainable value for a rod of length 'n'.","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java#L8-L44","documentation":"Thrown by RodCutting.cutRod(int[] price, int n) when n < 0. The rod length drives the DP array size (val = new int[n+1]); a negative n produces a negative-length array / negative indexing. Message: 'Rod length cannot be negative.'","triggerScenarios":"Passing a negative n; computing n from a subtraction that underflows; user input parsed without sign validation.","commonSituations":"n derived as (a - b) where b > a; CLI argument '--length=-5' accepted by a lenient parser.","solutions":["Clamp or reject n < 0 before the call (typically treat as 0 or error).","Validate numeric input at the parse boundary with a min-value check.","Unit-test the boundary (n = 0, n = 1) alongside the negative case."],"exampleFix":"// before\nint best = RodCutting.cutRod(prices, n);\n\n// after\nif (n < 0) throw new IllegalArgumentException(\"n must be >= 0\");\nint best = RodCutting.cutRod(prices, n);","handlingStrategy":"validation","validationCode":"if (n < 0) throw new IllegalArgumentException(\"n must be >= 0\");\nRodCutting.cutRod(price, n);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate numeric CLI/config input with a min-value check.","Treat negative lengths as input errors, not defaults.","Unit-test the n=0 and n<0 boundaries."],"tags":["input-validation","negative-values","dynamic-programming"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}