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.

    a3122-13645546-3738
    bit0102030405060708

    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.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Quiz

    20 questions
    Question 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.

    Keep going