If you are into competitive programming, then you have probably heard about the infamous prefix sum algorithm. But what exactly is it?
Essentially, prefix sum is an algorithm that goes through a linear list to produce another list where ith index stores the sum of all the elements from 0 to ith.
That is the gist of it. However, they can be abstracted into much more complex algorithms to solve interesting problems. Here is a more formal definition of prefix sum
A prefix sum, also known as a cumulative sum, is a sequence of partial sums of a given sequence. In other words, it is a way to transform a list of numbers into a list of sums, where each element reflects the sum of all the elements that come before it.
For example, consider the following list of numbers: [1, 2, 3, 4, 5]
A prefix sum of this list would be: [1, 3, 6, 10, 15]
This can be useful in a variety of situations, such as calculating running totals or performing cumulative operations on a list of numbers.
One way to calculate a prefix sum is to use a loop to iterate over the original list and add each element to the sum of the previous elements. In Python, this can be done as follows:
def prefix_sum(numbers):
prefix_sums = []
current_sum = 0
for number in numbers:
current_sum += number
prefix_sums.append(current_sum)
return prefix_sums
print(prefix_sum([1, 2, 3, 4, 5])) # [1, 3, 6, 10, 15]
Another way to calculate a prefix sum is to use the built-in accumulate function from the itertools module. This function takes an iterable and a function as arguments, and returns a generator that yields the cumulative results of applying the function to the items of the iterable.
For example, to calculate a prefix sum using the accumulate function, we can use the add function as the accumulation function:
from itertools import accumulate
def prefix_sum(numbers):
return list(accumulate(numbers, func=add))
print(prefix_sum([1, 2, 3, 4, 5])) # [1, 3, 6, 10, 15]
Both of these approaches have a time complexity of O(n), where n is the length of the list. This means that the prefix sum can be calculated in linear time, making it a relatively efficient algorithm for large lists.
Using prefix sum comes with its own pros and cons.
One example of a prefix sum algorithm is the cumulative sum algorithm. This algorithm can be used to calculate the cumulative sum of an array or list of numbers. Here is an example of how the cumulative sum algorithm could be implemented in Python:
def prefix_sum(arr):
n = len(arr)
prefix_sum = [0] * (n + 1)
for i in range(1, n + 1):
prefix_sum[i] = prefix_sum[i - 1] + arr[i - 1]
return prefix_sum
arr = [1, 2, 3, 4, 5]
print(prefix_sum(arr))
In this example, the input array is [1, 2, 3, 4, 5]
, and the output is the prefix sum array [0, 1, 3, 6, 10, 15]
. The prefix sum array can be used to quickly calculate the sum of any range of elements in the original array. For example, to calculate the sum of the elements from index 2 to index 4 (i.e. [3, 4, 5]
), we can use the prefix sum array to get the sum of the range by subtracting prefix_sum[2]
from prefix_sum[5]
(i.e. prefix_sum[5]
- prefix_sum[2]
= 15 - 3 = 12).
All in all, prefix sum is an algorithm that will often come in handy for developers. Many novice software developers often hear the term prefix sum and get under the impression that prefix sum algorithm is only relevant for competitive programmers. However, nevertheless, prefix sum is useful for all avenues in the programming world.