Prefix Sum
published
last modified
4kb
Prefix sums are a data structure that allow use to solve some interesting problems. A prefix sum array is an array based on a cumulative sum of another array. The value at is the sum of all elements of array up to the point of .
So for example for the array:
It's prefix sum will be:
How Can We Create Prefix Sums?
Notice how for any index , to compute the value at , we have to take the number at and add it to the value at .
One might notice that this definition demands attention for the very first element of the array. Technically at the prefix array is undefined. We have either two choices:
- Initiliase the prefix array with it's first value as zero.
- Initiliase the prefix array with the first value of the original array.
The choice of either will work, but have different names in terms of what the scan is called to produce the prefix array as well as the interpretation of in the prefix array. The first option is produced by an exclusive scan and the latter by a inclusive scan
Inclusive and Exclusive Scans
If we go with the first option, then we produce an array with an exclusive scan. What this means, is that at any position in the prefix sum that is the sum up to but not including .
For example, say we have an array:
We create a prefix sum array with zero as the seed value:
Let's say we take the index . The prefix sum at that index is . However if we look at the original array we can see that the actual sum at is . This is what we mean by exclusive, we don't include that number at in the original array at that position in the prefix array.
The converse, is a inclusive scan where we include in the sum.
For example, removing the seed value of zero:
For index , the sum is , which includes since
Use Case: Range Sum
A useful use case for prefix sums is it to calculate the sum of a range in time. If we have an array we can pre-compute the prefix sum and access any range sum in constant time.
We can use the formula, if we use an inclusive scan:
For instance, if sum of the array in the previous example from index the sum would be which is correct.
One gotcha is if . It's obvious that we would get an index error so if this is the case we would just return , which coincidentally is just the sum up until .
However if our prefix array generated using an exclusive scan, the formula is:
The same gotcha applies here if in which case we return .
Wrap Up
Hope you enjoyed reading this and have learnt something. There are many more use cases for prefix sums and they can be quite useful to solve a variety of problems. Thanks for reading. Happy coding.
Contact
If you have any comments or thoughts you can reach me at any of the places below.