TheAlgorithms/Java · error · IllegalArgumentException
Invalid range: [%d, %d] for array of size %d
Error message
Invalid range: [%d, %d] for array of size %d
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/prefixsum/DifferenceArray.java:66
}
}
/**
* Adds a value to all elements in the range [l, r].
*
* <p>
* This method uses a branchless approach by allocating an extra element at the end
* of the array, avoiding the conditional check for the right boundary.
* </p>
*
* @param l The starting index (inclusive).
* @param r The ending index (inclusive).
* @param val The value to add.
* @throws IllegalArgumentException if the range is invalid.
*/
public void update(int l, int r, int val) {
if (l < 0 || r >= n || l > r) {
throw new IllegalArgumentException(String.format("Invalid range: [%d, %d] for array of size %d", l, r, n));
}
differenceArray[l] += val;
differenceArray[r + 1] -= val;
}
/**
* Reconstructs the final array using prefix sums.
*
* @return The resulting array after all updates. Returns long[] to handle potential overflows.
*/
public long[] getResultArray() {
long[] result = new long[n];
result[0] = differenceArray[0];
for (int i = 1; i < n; i++) {
result[i] = differenceArray[i] + result[i - 1];
}View on GitHub (pinned to fdfb9a395b)
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().
Example fix
// before da.update(left, right, val); // caller uses exclusive right bound // after da.update(left, right - 1, val); // right is inclusive in update(); adjust at call site // or guard: if (left >= 0 && right - 1 < n && left <= right - 1) da.update(left, right - 1, val);
Defensive patterns
Strategy: validation
Validate before calling
// convert caller's exclusive end 'rightExclusive' to inclusive, then validate
int r = rightExclusive - 1;
if (l < 0 || r >= n || l > r) {
throw new IllegalArgumentException("range [" + l + "," + r + "] invalid for size " + n);
}
da.update(l, r, val); Type guard
static boolean validRange(int l, int rInclusive, int n) {
return l >= 0 && rInclusive < n && l <= rInclusive;
} Try / catch
try {
da.update(l, r, val);
} catch (IllegalArgumentException e) {
logger.warn("Skipping out-of-range difference update [{}, {}] for size {}", l, r, n);
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Input array cannot be null or empty.
- Invalid range indices
- 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/0dab476730b7467d.
Report an issue: GitHub.