Range Sum Query - Mutable

MediumRange Query · Fenwick Tree (BIT)🧩 Fenwick & Segment TreeLeetCode #307 ↗Updated

Problem

Design a class NumArray built from an integer array nums that supports two operations, mixed in any order: update(index, val) overwrites nums[index] with val, and sumRange(left, right) returns the sum of nums[left..right] (both ends inclusive).
InputNumArray([1,3,5]); sumRange(0,2); update(1,2); sumRange(0,2)Output[null, 9, null, 8]
InputNumArray([-4]); sumRange(0,0); update(0,7); sumRange(0,0)Output[null, -4, null, 7]

1 ≤ n ≤ 3·104  ·  -100 ≤ nums[i], val ≤ 100  ·  at most 3·104 calls to update and sumRange

Try it yourself

Java
class NumArray {
    public NumArray(int[] nums) {
        
    }

    public void update(int index, int val) {
        
    }

    public int sumRange(int left, int right) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
A plain prefix-sum array makes queries O(1) but updates O(n). A Fenwick tree stores partial sums over power-of-two blocks so that both a prefix query and a point update touch only O(log n) cells.
PatternFenwick & Segment Tree

Instinct Range sums, mins or counts asked again and again while values keep changing: prefix sums break, so reach for a Fenwick or segment tree.

Memory hack Prefix sums freeze, trees breathe: climb by the last set bit (i & -i) for Fenwick, split in halves for segment trees.

Open the Fenwick & Segment Tree guide →

Approaches

Brute ForcePlain Array, Sum on Demand

Time update O(1), sumRange O(n)Space O(n)

Keep a copy of the array. An update is a single assignment; a query loops from left to right adding values. Nothing is precomputed, so nothing goes stale.

Java
class NumArray {
    private final int[] nums;

    public NumArray(int[] nums) {
        this.nums = nums.clone();
    }

    public void update(int index, int val) {
        nums[index] = val;
    }

    public int sumRange(int left, int right) {
        int sum = 0;
        for (int i = left; i <= right; i++) sum += nums[i];
        return sum;
    }
}

Fine when queries are rare; 3·104 full-range queries cost ~109 adds.

At a glance

LevelApproachTimeSpace
Brute ForcePlain Array, Sum on Demandupdate O(1), sumRange O(n)O(n)
BetterSquare-Root Decompositionupdate O(1), sumRange O(√n)O(n)
OptimalFenwick Tree (Binary Indexed Tree)build O(n), update / sumRange O(log n)O(n)
Common pitfalls
  • Adding val instead of val - old to the tree on update.
  • Mixing 0-based array indices with the 1-based Fenwick tree (off-by-one).
  • Rebuilding a prefix-sum array on every update — back to O(n) per call.
Interview tips
  • Start from immutable prefix sums (303) and explain exactly why updates break them.
  • Offer √n decomposition as a quick middle ground before the BIT.
  • Mention a segment tree as the more general alternative (min/max, range updates).
Pattern
Fenwick / segment tree → interleaved point updates and range aggregate queries on an array.
My notes saved in this browser