{"record":{"id":"c5592fd4efded9f4","repo":"TheAlgorithms/Java","slug":"invalid-range-indices","errorCode":null,"errorMessage":"Invalid range indices","messagePattern":"Invalid range indices","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/prefixsum/PrefixSum.java","lineNumber":50,"sourceCode":"\n        for (int i = 0; i < array.length; i++) {\n            // Automatically promotes int to long during addition\n            this.prefixSums[i + 1] = this.prefixSums[i] + array[i];\n        }\n    }\n\n    /**\n     * Calculates the sum of elements in the range [left, right].\n     * Indices are 0-based.\n     *\n     * @param left  The starting index (inclusive).\n     * @param right The ending index (inclusive).\n     * @return The sum of elements from index left to right as a long.\n     * @throws IndexOutOfBoundsException if indices are out of valid range.\n     */\n    public long sumRange(int left, int right) {\n        if (left < 0 || right >= prefixSums.length - 1 || left > right) {\n            throw new IndexOutOfBoundsException(\"Invalid range indices\");\n        }\n        return prefixSums[right + 1] - prefixSums[left];\n    }\n}\n","sourceCodeStart":32,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/prefixsum/PrefixSum.java#L32-L55","documentation":"Thrown by PrefixSum.sumRange(left, right) when the query bounds are invalid: left < 0, right >= array length, or left > right. This is an IndexOutOfBoundsException because the indices would read past the prefix-sum buffer. Bounds are 0-based inclusive against the ORIGINAL array length (prefixSums.length - 1).","triggerScenarios":"Call sumRange(0, array.length) (right past last index), sumRange(-1, 3), or sumRange(5, 2).","commonSituations":"Passing an exclusive end as inclusive (off-by-one); 1-based indices not converted; right computed as a list size rather than last index.","solutions":["Pass inclusive 0-based indices: right should be lastIndex (length-1), not length.","Convert 1-based input to 0-based before calling.","Guard left >= 0 && right < array.length && left <= right at the caller."],"exampleFix":"// before\nlong s = ps.sumRange(start, end); // end is exclusive here\n\n// after\nlong s = ps.sumRange(start, end - 1); // end converted to inclusive index\n// guard:\nif (start < 0 || end - 1 >= n || start > end - 1) throw new IllegalArgumentException(\"bad range\");","handlingStrategy":"validation","validationCode":"int n = array.length; // original data length\nif (left < 0 || right >= n || left > right) {\n    throw new IndexOutOfBoundsException(\"range [\" + left + \",\" + right + \"] invalid for size \" + n);\n}\nlong s = ps.sumRange(left, right);","typeGuard":"static boolean validQuery(int left, int rightInclusive, int dataSize) {\n    return left >= 0 && rightInclusive < dataSize && left <= rightInclusive;\n}","tryCatchPattern":"try {\n    long s = ps.sumRange(left, right);\n} catch (IndexOutOfBoundsException e) {\n    logger.warn(\"Bad range query [{}, {}]\", left, right);\n}","preventionTips":["Track the original data length; the prefix array is one longer (n+1).","Use inclusive end indices; if your caller uses exclusive, subtract one at the call site.","Convert 1-based indices at the system boundary."],"tags":["prefix-sum","input-validation","index-out-of-bounds","off-by-one"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}