Fenwick tree explained in plain terms means understanding how a single array, with no pointers and no nodes, can answer prefix sums in logarithmic time. The central question is how such a tiny structure can rival a full segment tree, and the answer is a trick of binary arithmetic: each position stores the sum of a carefully chosen range, and the ranges fit together like tiles.
A Fenwick tree, also called a binary indexed tree, stores an array in a way that makes the sum of the first n elements, and the update of a single element, both cost logarithmic time. It was published by Peter Fenwick in 1994 for arithmetic coding, and it has since become a staple of competitive programming and of any system that counts things as they arrive.
The idea is small enough to fit in a pocket and powerful enough to replace a tree ten times its size for the problems it suits. This article explains what a Fenwick tree is, how it works, and where it shines.
Key takeaways
- A Fenwick tree is a flat array that answers prefix sums in logarithmic time.
- Point updates are logarithmic too, using the same bit trick.
- It uses no extra memory beyond the array itself.
- Range sums come from subtracting two prefix sums.
- It suits sums and counts, not range minimums or maximums.
What a Fenwick tree is
A Fenwick tree is an array where each position holds the sum of a range of the original array, and the length of that range is determined by the lowest set bit of the position’s index. Position eight, for example, holds the sum of the first eight elements, position six holds the sum of elements five and six, and position seven holds just element seven. The pattern looks odd at first, and it is exactly what makes the structure work.
The magic is that any prefix can be built from a few of these stored ranges, and any update can be spread to a few of them. To find the sum of the first thirteen elements, the tree adds the stored ranges for positions thirteen, twelve, and eight, three steps for a prefix of thirteen. To update element five, the tree adds the change to positions five, six, eight, and sixteen, four steps that cover every range containing element five.
The rule that decides which positions to visit is the lowest set bit, the smallest power of two that divides the index. Moving to the next position in a query means clearing that bit, and moving to the next position in an update means adding it, which is why the whole structure reduces to a couple of lines of bit arithmetic. Our guide to hashing covers another compact structure that trades a little cleverness for a lot of speed.
How queries and updates work
A prefix sum query starts at the wanted index and walks downward, adding the stored value at each stop and clearing the lowest set bit to find the next stop. Each step at least halves the remaining distance, which is why the walk takes logarithmic time, and the total it accumulates is exactly the sum of the first n elements. A range sum is then just two prefix sums subtracted, the sum up to the end minus the sum before the start.
A point update starts at the changed index and walks upward, adding the difference to each stored range it visits and adding the lowest set bit to find the next one. Every range that contains the changed element gets corrected, and no other range is touched, so the tree stays correct after logarithmically few steps. The symmetry between the two walks is part of the charm: query goes down by clearing bits, update goes up by adding them.
Building the tree from scratch costs linearithmic time if each element is inserted one by one, or linear time with a smarter pass that propagates each value upward once. In practice the build is rarely the bottleneck, because the tree is usually built once and then queried thousands of times, and even the slower build is cheap next to the work it saves.
The tree can also find the smallest index whose prefix sum reaches a target, which is the operation behind order statistics and weighted random sampling. The search walks down the tree from the largest power of two, deciding at each step whether the target lies in the left or right half, and it runs in logarithmic time just like everything else the structure does.

Fenwick tree versus segment tree
The Fenwick tree and the segment tree are often presented as rivals, and the honest answer is that they are tools for overlapping but different jobs. The Fenwick tree is smaller, simpler, and faster for the problems it can solve, while the segment tree is the generalist that handles everything else. Choosing between them means asking what operations the problem actually needs.
The table below sets out the main contrasts between the leading approaches.
| Aspect | Fenwick tree | Segment tree | Prefix sums |
|---|---|---|---|
| Prefix sum query | Logarithmic | Logarithmic | Constant |
| Range sum query | Logarithmic, via two prefixes | Logarithmic | Constant |
| Range minimum query | Not supported in general | Logarithmic | Not supported |
| Point update | Logarithmic | Logarithmic | Linear rebuild |
| Memory used | Same as the array | About four times the array | Same as the array |
| Best for | Sums and counts with updates | General range queries | Static data, read-only |
The practical result is that a Fenwick tree is the right pick whenever the operation is a sum or a count and the queries are prefixes or ranges, while the segment tree is the pick when minimums, maximums, or range updates enter the picture.
Where Fenwick trees are used
Competitive programming is where most people meet the Fenwick tree, and it is the standard answer for counting inversions, tracking frequencies, and answering range sum queries under updates. Problems that ask how many pairs are out of order, or how many elements fall in a range, reduce to prefix sums, and the Fenwick tree answers them in logarithmic time with a dozen lines of code.
Arithmetic coding, the original application, uses a Fenwick tree to maintain symbol frequencies as a compressor works through a file. The encoder needs the cumulative frequency of each symbol to place it in the coding interval, and it needs to update frequencies as symbols appear, which is precisely a prefix sum with point updates.
Databases and analytics engines use Fenwick-style structures for running totals and for counting events in time windows. A dashboard showing sign-ups per hour, or a system counting requests in the last minute, is maintaining prefix sums over a stream, and the same bit trick keeps those counts cheap. Our guide to database indexing explains how ordered structures make such queries fast at scale.
Games and simulations use Fenwick trees for weighted random selection, where the chance of picking an item is proportional to its weight. The tree finds the item whose cumulative weight crosses a random threshold in logarithmic time, which keeps loot drops, procedural generation, and randomised testing fast even with huge item pools.
Strengths and limits
The greatest strength of a Fenwick tree is its economy. It needs no extra memory, no pointers, and no nodes, just the array itself plus a few lines of bit arithmetic, and it still delivers logarithmic queries and updates. That simplicity makes it fast in practice as well as in theory, because the data sits in a flat array that the processor’s cache loves.
It is also a strength that the code is tiny. A correct Fenwick tree fits in about ten lines, which means fewer bugs, easier review, and a structure that can be written from memory in an interview or a contest. Few data structures offer that ratio of power to code.
The limits are real, though. A Fenwick tree cannot answer range minimums or maximums in general, because those operations cannot be recovered by subtracting two prefixes, and it has no lazy propagation for range updates. Our guide to cache invalidation is a reminder that even the fastest structure needs care when the underlying data changes, and the Fenwick tree is no exception.
Fenwick tree checklist
- Use a Fenwick tree when the operation is a sum or a count.
- Remember the tree is one-indexed, so shift indices by one.
- Answer range sums by subtracting two prefix sums.
- Use the lowest set bit to move between positions.
- Prefer a segment tree for range minimums and maximums.
- Prefer prefix sums when the array never changes.
- Test queries on prefixes of length one and the full array.
- Use the order-statistic search for weighted random picks.
- Build in linear time when loading a large array at once.
Frequently asked questions
What is a Fenwick tree in simple terms?
It is a flat array that stores a clever set of partial sums, so any prefix sum or point update takes logarithmic time with no extra memory.
Who invented the Fenwick tree?
Peter Fenwick, a New Zealand computer scientist, published it in 1994 for use in arithmetic coding, and it is also called a binary indexed tree.
What is the lowest set bit trick?
It is the rule that each index stores the sum of a range whose length is the smallest power of two dividing the index, which decides which positions a query or update visits.
Can a Fenwick tree find a range minimum?
Not in general, because a minimum cannot be recovered by subtracting two prefix minimums, so a segment tree is the right tool for that job.
How fast is a Fenwick tree query?
Logarithmic in the size of the array, with a very small constant, because each step at least halves the distance left to travel.
When should I not use a Fenwick tree?
When the operation is a minimum or maximum, or when range updates are needed, where a segment tree with lazy propagation fits better.
The bottom line
Fenwick tree explained in one line is this: store a few well-chosen partial sums, and any prefix question becomes a short walk guided by bit arithmetic.
It is the smallest structure that delivers logarithmic range sums with updates, and the reason so many counting problems have such short solutions.
This article is general information about how Fenwick trees work, not advice about choosing a data structure for a particular system.







