Complete the code to initialize a Fenwick tree array of size n+1.
fenwicks = [0] * [1]
The Fenwick tree uses 1-based indexing, so the array size is n+1 to include index n.
Complete the code to get the least significant bit (LSB) of an integer x.
lsb = x & [1]The expression x & (-x) isolates the least significant bit set to 1 in x.
Fix the error in the Fenwick tree update function to correctly move to the next index.
def update(i, delta): while i < len(fenwicks): fenwicks[i] += delta i += [1]
To move to the next index in Fenwick tree update, add the least significant bit (i & -i) to i.
Fill both blanks to compute the prefix sum up to index i in a Fenwick tree.
def prefix_sum(i): result = 0 while i > 0: result += fenwicks[[1]] i -= [2] return result
To get the prefix sum, add fenwicks[i] and then move to the parent by subtracting the LSB (i & -i).
Fill all three blanks to create a dictionary comprehension that maps each index to its Fenwick tree prefix sum if the sum is positive.
result = [1]: prefix_sum([2]) for [3] in range(1, n+1) if prefix_sum([2]) > 0}
The comprehension uses i as the key and argument to prefix_sum, iterating i from 1 to n.