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.

Let ‘s say, we are given an array of size N and Q queries of two types:

  • Type 1, where we are given two integers L and R and we need to calculate the sum of elements in range L to R.
  • Type 2, where we are given two integers I and X and we need to update the element at index I to X.

Now, we can solve the question using the following approaches:

  • Brute Force: For every query of Type 1, we run a loop from L to R and calculate the sum and whenever we encounter a query of type 2, we simply update the element at that index. Each Type 2 operation is done in O(1) time but each Type 1 operation can take O(N) time in worst case. So, the overall time complexity will be O(Q * N), which is very costly. This approach is only beneficial when the number of updates are very large as compared to the number of range queries.
  • Precomputation Techniques: We can maintain a 2-D matrix of size N*N and store the range sum of every possible range in the matrix. Now, every query of type 1 can be done in O(1) time but every type 2 query can take O(N*N) time in worst case. So, the overall time complexity will be O(Q * N * N), which is also very costly. This approach is only beneficial when the number of updates are very less as compared to the number of range queries.
  • Fenwick Trees or Binary Indexed Trees(BIT): Using Fenwick trees, we can guarantee a log(N) time for both the type of queries. So, the time complexity will be O(q * log(N)). This approach is beneficial if the number of range queries and updates are comparable to each other.

Note: We can also use Segment Tree and solve both updates and range queries in logarithmic time but its faster to implement Fenwick trees as compared to Segment trees as Fenwick trees are easy to build and query and require very less time to code. Also, Fenwick Tree require less space as compared to Segment Tree.

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