trekhleb/javascript-algorithms · error · Error

Position is out of allowed range

Error message

Position is out of allowed range

What it means

FenwickTree.increase(position, value) adds value at a 1-based position and propagates it through the internal array using i += (i & -i). Valid positions are exactly 1..arraySize (the size given to the constructor); anything below 1 or above arraySize throws immediately. The guard exists because an out-of-range position would otherwise silently corrupt the treeArray indices shared by increase and query.

Source

Thrown at src/data-structures/tree/fenwick-tree/FenwickTree.js:24

   * @param  {number} arraySize
   */
  constructor(arraySize) {
    this.arraySize = arraySize;

    // Fill tree array with zeros.
    this.treeArray = Array(this.arraySize + 1).fill(0);
  }

  /**
   * Adds value to existing value at position.
   *
   * @param  {number} position
   * @param  {number} value
   * @return {FenwickTree}
   */
  increase(position, value) {
    if (position < 1 || position > this.arraySize) {
      throw new Error('Position is out of allowed range');
    }

    for (let i = position; i <= this.arraySize; i += (i & -i)) {
      this.treeArray[i] += value;
    }

    return this;
  }

  /**
   * Query sum from index 1 to position.
   *
   * @param  {number} position
   * @return {number}
   */
  query(position) {
    if (position < 1 || position > this.arraySize) {
      throw new Error('Position is out of allowed range');

View on GitHub (pinned to 85293e3e2b)

Solutions

  1. Convert your 0-based index before calling: increase(i + 1, value).
  2. If the failing index is above arraySize, re-check the size passed to new FenwickTree(n) — it must be at least your largest 1-based position.
  3. Validate before calling: Number.isInteger(position) && position >= 1 && position <= tree.arraySize.

Example fix

// before
const ft = new FenwickTree(arr.length);
for (let i = 0; i < arr.length; i += 1) {
  ft.increase(i, arr[i]); // throws: position 0 is below 1
}

// after
for (let i = 0; i < arr.length; i += 1) {
  ft.increase(i + 1, arr[i]); // 1-based position
}
Defensive patterns

Strategy: validation

Validate before calling

const isValidFenwickPosition = (position, tree) =>
  Number.isInteger(position) && position >= 1 && position <= tree.arraySize;

function safeIncrease(tree, position, value) {
  if (!isValidFenwickPosition(position, tree)) {
    throw new RangeError(`position must be in [1, ${tree.arraySize}], got ${position}`);
  }
  return tree.increase(position, value);
}

Prevention

When it happens

Trigger: Calling increase(0, v) because your source array is 0-indexed; calling increase(n, v) where n equals the element count but the tree was built with a smaller size; passing a computed index such as right + 1 that overflows arraySize; passing undefined or NaN which compares out of range.

Common situations: Wrapping a Fenwick tree over a 0-based array without adding 1; porting segment-tree code (typically 0-based) to a binary indexed tree; competitive-programming templates whose loop bounds were copied from a differently sized problem; constructing the tree with new FenwickTree(arr.length - 1) by mistake.

Related errors


AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24). Data as JSON: /api/errors/f39dfa67de78cd12. Report an issue: GitHub.