{"record":{"id":"0dab476730b7467d","repo":"TheAlgorithms/Java","slug":"invalid-range-d-d-for-array-of-size-d","errorCode":null,"errorMessage":"Invalid range: [%d, %d] for array of size %d","messagePattern":"Invalid range: \\[(.+?), (.+?)\\] for array of size (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/prefixsum/DifferenceArray.java","lineNumber":66,"sourceCode":"        }\n    }\n\n    /**\n     * Adds a value to all elements in the range [l, r].\n     *\n     * <p>\n     * This method uses a branchless approach by allocating an extra element at the end\n     * of the array, avoiding the conditional check for the right boundary.\n     * </p>\n     *\n     * @param l   The starting index (inclusive).\n     * @param r   The ending index (inclusive).\n     * @param val The value to add.\n     * @throws IllegalArgumentException if the range is invalid.\n     */\n    public void update(int l, int r, int val) {\n        if (l < 0 || r >= n || l > r) {\n            throw new IllegalArgumentException(String.format(\"Invalid range: [%d, %d] for array of size %d\", l, r, n));\n        }\n\n        differenceArray[l] += val;\n        differenceArray[r + 1] -= val;\n    }\n\n    /**\n     * Reconstructs the final array using prefix sums.\n     *\n     * @return The resulting array after all updates. Returns long[] to handle potential overflows.\n     */\n    public long[] getResultArray() {\n        long[] result = new long[n];\n        result[0] = differenceArray[0];\n\n        for (int i = 1; i < n; i++) {\n            result[i] = differenceArray[i] + result[i - 1];\n        }","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/prefixsum/DifferenceArray.java#L48-L84","documentation":"Thrown by DifferenceArray.update(l, r, val) when the update range is out of bounds: l < 0, r >= n, or l > r (r and l are inclusive 0-based indices into the original array). The branchless implementation writes to differenceArray[r + 1], so an out-of-range r would overflow the buffer.","triggerScenarios":"Call update(-1, 3, 5), update(0, n, 5) (r equals the array length, one past the last index), or update(5, 2, 5) (start after end).","commonSituations":"Off-by-one when computing r as an exclusive bound and passing it as inclusive (r = n instead of n-1); inverted loop bounds; user-supplied 1-based indices passed without converting to 0-based.","solutions":["Convert any exclusive end bound to inclusive: pass r-1 if your caller uses exclusive ranges.","Convert any 1-based indices to 0-based before calling.","Validate l >= 0 && r < n && l <= r at the caller and clamp or reject before update()."],"exampleFix":"// before\nda.update(left, right, val); // caller uses exclusive right bound\n\n// after\nda.update(left, right - 1, val); // right is inclusive in update(); adjust at call site\n// or guard:\nif (left >= 0 && right - 1 < n && left <= right - 1) da.update(left, right - 1, val);","handlingStrategy":"validation","validationCode":"// convert caller's exclusive end 'rightExclusive' to inclusive, then validate\nint r = rightExclusive - 1;\nif (l < 0 || r >= n || l > r) {\n    throw new IllegalArgumentException(\"range [\" + l + \",\" + r + \"] invalid for size \" + n);\n}\nda.update(l, r, val);","typeGuard":"static boolean validRange(int l, int rInclusive, int n) {\n    return l >= 0 && rInclusive < n && l <= rInclusive;\n}","tryCatchPattern":"try {\n    da.update(l, r, val);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Skipping out-of-range difference update [{}, {}] for size {}\", l, r, n);\n}","preventionTips":["Document at the call site whether your end bound is inclusive or exclusive; DifferenceArray.update is inclusive.","Convert 1-based external indices to 0-based at the boundary.","Keep the original array length n accessible where you call update."],"tags":["prefix-sum","input-validation","illegal-argument","off-by-one","data-structure"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}