Idea behind Fenwick Trees

Consider Fenwick tree as an array F[] with 1-based indexing, such that every index of array stores Partial Sum of range. For simplicity, assume the size of F[] to be 16. Now, if we have queries to calculate the range sum in an array. So, for any index i, F[i] will store the sum of values from index j+1 to i, such that j = i – (rightmost set bit of i). Refer to the below table to understand the range of values:

Value of i

Binary Representation

Rightmost set bit of i

Value of j

Partial Sum Range [start, end]

0

0000

0000

0000

1

0001

0001

0000

[1, 1]

2

0010

0010

0000

[1, 2]

3

0011

0001

0010

[3, 3]

4

0100

0100

0000

[1, 4]

5

0101

0001

0100

[5, 5]

6

0110

0010

0100

[5, 6]

7

0111

0001

0110

[7, 7]

8

1000

1000

0000

[1, 8]

9

1001

0001

1000

[9, 9]

10

1010

0010

1000

[9, 10]

11

1011

0001

1010

[11, 11]

12

1100

0100

1000

[9, 12]

13

1101

0001

1100

[13, 13]

14

1110

0010

1100

[13, 14]

15

1111

0001

1110

[15, 15]

So, from the above table we can see that each index of Fenwick tree F[] stores the sum of range. Now, if we need to calculate the prefix sum till 7th element, then instead of iterating from 1 to 7 and calculating the sum, we can take the sum of F[7] + F[6] + F[4]. Similarly, to calculate the prefix sum till 13th element, we can take the sum of F[13] + F[12] + F[8] to get the prefix sum. So, in other words if we want to calculate the prefix sum till ith element, we can add elements of array F[] present at indices which are obtained by removing the last set bit of i, one by one.

Prefix Sum till 7th element = F[7] + F[6] + F[4],
7 –remove right most set bit –> 6 –remove right most set bit –> 4 –remove right most set bit –> 0

Prefix Sum till 13th element = F[13] + F[12] + F[8],
13 –remove right most set bit –> 12 –remove right most set bit –> 8 –remove right most set bit –> 0

Now, in order to calculate the range sum of [L, R], we can simply subtract the prefix sum till [L-1] from prefix sum till R.

Fenwick Tree (Binary Indexed Tree) for Competitive Programming

In the world of competitive programming, speed is everything. Fenwick Tree (also known as Binary Indexed Tree), created by Peter M. Fenwick. They’re like secret weapons for programmers, especially when you need to quickly find cumulative frequencies in problem-solving. This article breaks down Fenwick Trees in simple terms—how they’re built and why they’re handy for competitive programming. Whether you’re starting out or a pro looking for a speed boost, Fenwick Trees could be your key to success in coding competitions. Let’s dive in!

Fenwick Tree for Competitive Programming

Similar Reads

What is Fenwick Tree?

Fenwick Tree or Binary Indexed Tree is a data structure used to calculate range queries along with updating the elements of the array, such that each query or update takes logarithmic time complexity. It is used to calculate prefix sums or running total of values up to any index....

Why to use Fenwick Tree?

Whenever we have range queries along with updating the elements of the array such that the number of range queries are comparable to the number of updates in the array, then we can perform both of these operations in logarithmic time using Fenwick Trees....

Idea behind Fenwick Trees:

...

Fenwick Tree Implementation for Range Queries and Updates:

Since, we are storing Partial Sums of range in the Fenwick Tree F[], then updating an element at index i in the original array should reflect in all those indices of F[] which have index i in their partial sum range. For eg: if we want to update the element at index 5 in the original array, then it should reflect in F[5], F[6] and F[8] because F[5] stores the partial sum of range [5, 5], F[6] stores the partial sum of range [5, 6] and F[8] stores the partial sum of range [1, 8] and 5 lies in the range of all of them....

Building the Fenwick Tree in Linear Time Complexity:

In the above approach, we are building the Fenwick tree by calling the add() method for all the elements so the overall complexity to build the tree becomes O(N * logN). We can further reduce the time to build the Fenwick tree from O(N * logN) to O(N). Note that for every index i, we are adding a[i] to F[i], F[i + (i & (-i))] and so on. Also, later in the loop we have to add a[i + (i & (-i))] to all these indices again (except index i). So, instead of doing the operation repeatedly, we can combine them in one operation for every index. For every index i, we can simply add a[i] to F[i] and add F[i] to F[i + (i & (-i))]. Adding F[i] to F[i + (i & (-i))] will work same as adding a[i] to F[i + (i & (-i))] and all other indices....

Fenwick Tree (Binary Indexed Tree) for multi-dimensional arrays:

For 2-D arrays, we can use the same idea as 1-D array. Here, instead of storing Partial Sums of range [l to r], we store the Partial Sum of submatrix [l1 to r1][l2 to r2]. Refer this article to explore further about 2-D Fenwick Tree....

Use Cases of Fenwick Tree for Competitive Programming:

A Fenwick tree can support the following range operations:...

Practice Problems on Fenwick Tree for Competitive Programming:

Problem Problem Link Count Inversions Practice Now Easy Task Practice Now Array Partition Practice Now Range Minimum Query Practice Now Nitika and her queries Practice Now Maximum prefix sum for a given range Practice Now Smallest Subarray with given GCD Practice Now Element left after performing alternate OR & XOR operation Practice Now Easy Query Practice Now Akku and Arrays Practice Now Greater or Less Practice Now Sum and Update Queries on 3D Matrix in log(N) time Practice Now...

Contact Us