AdvancedAbout 30 min
Fenwick Tree (Binary Indexed Tree)
Read the full article on Substack
7 min read.
A simpler, more space-efficient alternative to Segment Tree for prefix sums and point updates. Uses bit manipulation for elegant O(log n) operations.
Think of it this way: Like a skip list for sums. Instead of storing every prefix sum, you store clever partial sums at specific positions. Using binary representation, you can 'hop' through just log(n) positions to get any prefix sum.
Build the tree by adding each value with update(); 8 values.
1/12
Check your understanding
Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.
Prefix sum query
Sequence
Order the steps of query(i) on a Fenwick tree.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
5 pieces left to place
Quiz
20 questionsQuestion 1 of 20
Score: 0
What does i & (-i) compute?
Done with this lesson?
Sign in free to keep your progress, streak and quiz scores.