AlgorithmsCodingDataStructures
Given an array of arr
, the prefix sum or cumulative sum is the sum of all prefixes in arr
, i.e. elements in arr
up to index
prefix_sum = [sum(arr[:i+1]) for i in range(n)]
Computational Complexity:
Time Complexity: O(n)
Space Complexity: O(n)
Access: O(1)
Any subarray sum can be computed in f(x)
than the sum:
prefix_sum = [f(arr[:i+1]) for i in range(n)]
For infinite sequences, prefix sums are just the partial sums of the sequence’s series. Partial summing is a linear operator
The prefix sum can be generalized to