Fenwick trees and dynamic prefix sums

Which indexes are read using i -= i & -i?

Examples

Worked example

Input
A query begins at one-based index 6.
Expected output
6, then 4

Hints and solution

Hint 1

Write down the starting state and walk through one example by hand.

Reveal worked answer

The lowest set bit of 6 is 2, so the next index is 4. The lowest set bit of 4 is 4, which moves the query to 0.

Back to the lesson