Data Structures · Visualization

Fenwick Tree: Prefix Sum

Fenwick Tree

Computing a prefix sum (the total of all elements from the start up to a given index) is done by starting at that index and repeatedly jumping backward using 'index -= lowest set bit of index', adding up each position visited along the way. Time: O(log n). Space: O(1) extra.

Big-O: Update and prefix-sum queries both O(log n).

Fenwick Tree: Prefix Sum

Fenwick Tree

Computing a prefix sum (the total of all elements from the start up to a given index) is done by starting at that index and repeatedly jumping backward using 'index -= lowest set bit of index', adding up each position visited along the way. Because each jump skips over a whole pre-summed range, only about log(n) positions need to be added.

How Prefix Sum Works

  1. Start at the target index (1-indexed) with a running total of 0.
  2. Add the value stored at the current position to the running total.
  3. Move to the previous position by subtracting the lowest set bit of the current index.
  4. Repeat until the index reaches 0.

Prefix Sum Worked Example

Input: Compute prefix sum up to index 6 in a Fenwick tree

  1. Add value at position 6, jump to 6 - lowestSetBit(6)=2 → position 4
  2. Add value at position 4, jump to 4 - lowestSetBit(4)=4 → position 0
  3. Index reached 0, stop and return the running total

Output: Sum of the first 6 elements

Try your own input
Fenwick Tree: Introduction
Also called a Binary Indexed Tree — a compact array structure where each index stores a partial sum over a range determined by its lowest set bit. Simpler than a segment tree, but built for prefix sums specifically.
3
0
2
1
6
2
1
3
4
4
5
5
7
6
2
7
Active BIT slot
Fenwick Tree: Introduction
Press ▶ Run to watch it step by step.
Python · Fenwick Trees in Code
def prefix_sum(bit, i):
    s = 0
    while i > 0:
        s += bit[i]
        i -= i & -i        # walk down
    return s

# Sum of a range = prefix(r) - prefix(l-1)
# O(log n)
# example: prefix sum 6
Input: Compute prefix sum up to index 6 in a Fenwick tree

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Prefix Sum Time & Space Complexity

TimeO(log n)
SpaceO(1) extra

When to Use Prefix Sum

  • Answering 'total so far' queries instantly on frequently changing data
  • Computing range sums by subtracting two prefix sums (prefixSum(r) - prefixSum(l-1))
  • Counting elements less than a value seen so far (used in counting inversions)

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.