TheAlgorithms/Java · error · IllegalArgumentException
Invalid range indices
Error message
Invalid range indices
What it means
Thrown by RangeSumQuery.sumRange(prefixSum, left, right) when the range is invalid: left < 0, right >= prefixSum.length - 1, or left > right. Note this throws IllegalArgumentException (not IndexOutOfBoundsException like PrefixSum.sumRange does) — an inconsistency between the two classes. Bounds are inclusive and measured against the ORIGINAL array length (prefixSum.length - 1).
Source
Thrown at src/main/java/com/thealgorithms/prefixsum/RangeSumQuery.java:69
return prefixSum;
}
/**
* Returns the sum of elements from index left to right (inclusive)
* using the provided prefix sum array.
*
* @param prefixSum The prefix sum array computed using buildPrefixSum.
* @param left The start index (inclusive).
* @param right The end index (inclusive).
* @return The sum of elements in the range [left, right].
* @throws IllegalArgumentException if indices are invalid.
*/
public static int sumRange(int[] prefixSum, int left, int right) {
if (prefixSum == null) {
throw new IllegalArgumentException("Prefix sum array cannot be null");
}
if (left < 0 || right >= prefixSum.length - 1 || left > right) {
throw new IllegalArgumentException("Invalid range indices");
}
return prefixSum[right + 1] - prefixSum[left];
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Remember prefixSum has length n+1: valid right is at most prefixSum.length - 2 for a data array of size n.
- Track the ORIGINAL data length separately and validate right < dataLength.
- Use the PrefixSum class instead, which encapsulates the length and avoids this confusion.
Example fix
// before int s = RangeSumQuery.sumRange(ps, left, right); // right miscomputed against ps.length // after int s = RangeSumQuery.sumRange(ps, left, Math.min(right, ps.length - 2)); // data length = ps.length - 1
Defensive patterns
Strategy: validation
Validate before calling
int dataLen = prefixSum.length - 1; // prefix array is n+1
if (left < 0 || right >= dataLen || left > right) {
throw new IllegalArgumentException("range invalid for data length " + dataLen);
}
int s = RangeSumQuery.sumRange(prefixSum, left, right); Type guard
static boolean validStaticRange(int left, int rightInclusive, int prefixArrayLength) {
int dataLen = prefixArrayLength - 1;
return left >= 0 && rightInclusive < dataLen && left <= rightInclusive;
} Try / catch
try {
int s = RangeSumQuery.sumRange(prefixSum, left, right);
} catch (IllegalArgumentException e) {
logger.warn("Bad static range query");
} Prevention
- Remember prefixSum.length == dataLen + 1; never use it directly as the data length.
- Track the original data length alongside the prefix array.
- Consider the stateful PrefixSum class to avoid this off-by-one footgun.
When it happens
Trigger: Call sumRange(ps, 0, ps.length - 1) treating the prefix array's own length as the data length; sumRange(ps, -1, 2); sumRange(ps, 5, 2).
Common situations: Confusing the prefix-sum array length (n+1) with the data length (n); passing an exclusive end as inclusive; 1-based indices not converted.
Related errors
- Invalid range: [%d, %d] for array of size %d
- Input array cannot be null or empty.
- Input array cannot be null
- Invalid range indices
- Input matrix cannot be null or empty
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/830af27fbd0e87cd.
Report an issue: GitHub.