TheAlgorithms/Java · error · IndexOutOfBoundsException
Invalid range indices
Error message
Invalid range indices
What it means
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).
Source
Thrown at src/main/java/com/thealgorithms/prefixsum/PrefixSum.java:50
for (int i = 0; i < array.length; i++) {
// Automatically promotes int to long during addition
this.prefixSums[i + 1] = this.prefixSums[i] + array[i];
}
}
/**
* Calculates the sum of elements in the range [left, right].
* Indices are 0-based.
*
* @param left The starting index (inclusive).
* @param right The ending index (inclusive).
* @return The sum of elements from index left to right as a long.
* @throws IndexOutOfBoundsException if indices are out of valid range.
*/
public long sumRange(int left, int right) {
if (left < 0 || right >= prefixSums.length - 1 || left > right) {
throw new IndexOutOfBoundsException("Invalid range indices");
}
return prefixSums[right + 1] - prefixSums[left];
}
}
View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
long s = ps.sumRange(start, end); // end is exclusive here
// after
long s = ps.sumRange(start, end - 1); // end converted to inclusive index
// guard:
if (start < 0 || end - 1 >= n || start > end - 1) throw new IllegalArgumentException("bad range"); Defensive patterns
Strategy: validation
Validate before calling
int n = array.length; // original data length
if (left < 0 || right >= n || left > right) {
throw new IndexOutOfBoundsException("range [" + left + "," + right + "] invalid for size " + n);
}
long s = ps.sumRange(left, right); Type guard
static boolean validQuery(int left, int rightInclusive, int dataSize) {
return left >= 0 && rightInclusive < dataSize && left <= rightInclusive;
} Try / catch
try {
long s = ps.sumRange(left, right);
} catch (IndexOutOfBoundsException e) {
logger.warn("Bad range query [{}, {}]", left, right);
} Prevention
- 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.
When it happens
Trigger: Call sumRange(0, array.length) (right past last index), sumRange(-1, 3), or sumRange(5, 2).
Common situations: Passing an exclusive end as inclusive (off-by-one); 1-based indices not converted; right computed as a list size rather than last index.
Related errors
- Invalid row indices
- Invalid column indices
- Invalid range: [%d, %d] for array of size %d
- Invalid range indices
- The index must not be negative.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/c5592fd4efded9f4.
Report an issue: GitHub.