Prefix Sum Pattern
Prefix Sum
Problem Scenario / Typical Use Case
Imagine you are analyzing daily sales data for a store over a year, and you want to quickly compute the total sales between any two given days.
A naive approach would sum the elements every time a query comes, which is inefficient for multiple queries. The Prefix Sum pattern allows you to precompute cumulative sums, so that any range sum can be retrieved in constant time.
Basic Idea
The Prefix Sum pattern involves creating an auxiliary array where each element stores the sum of all elements up to that index:
[ prefix[i] = arr[0] + arr[1] + ... + arr[i] ]
Once the prefix sum array is built, the sum of elements between indices i and j can be computed as:
[ sum(i, j) = prefix[j] - prefix[i-1] ]
Typical operations include:
- Calculating range sums efficiently.
- Detecting cumulative patterns in sequences.
- Optimizing repetitive summation in arrays or matrices.
Advantages
- Efficiency: After precomputation, any range sum query is (O(1)).
- Simplicity: Conceptually easy to understand and implement.
- Versatility: Works with arrays, 2D grids, and even strings for cumulative counts.
Limitations / Considerations
- Requires extra memory proportional to the size of the input.
- Only works efficiently for static or rarely updated data; updates may require recomputation or advanced techniques like Fenwick Trees.
- Careful indexing is necessary to avoid off-by-one errors.
Practical Examples / Thought Triggers
- Sales Between Two Dates: Precompute daily cumulative sales to answer multiple queries efficiently.
- Range Queries in Arrays: Compute sums, averages, or counts over subarrays without iterating each time.
- 2D Grids / Image Processing: Use 2D prefix sums to calculate sums over rectangular regions quickly.
Thought Triggers:
- Can this be combined with Sliding Window for variable-size subarrays?
- Is the data updated frequently? If yes, consider advanced structures like Fenwick Tree or Segment Tree.
- Can prefix sums help detect patterns or anomalies efficiently in time series or grids?
Implementation Example
Here’s a Python example for range sum queries using prefix sum:
def build_prefix_sum(arr):
prefix = [0] * len(arr)
prefix[0] = arr[0]
for i in range(1, len(arr)):
prefix[i] = prefix[i - 1] + arr[i]
return prefix
def range_sum(prefix, start, end):
if start == 0:
return prefix[end]
return prefix[end] - prefix[start - 1]
# Example usage
sales = [100, 200, 150, 300, 250]
prefix = build_prefix_sum(sales)
print(range_sum(prefix, 1, 3)) # Output: 650 (sum of days 2-4)
This shows how precomputing cumulative sums allows efficient range queries without repeated iteration.
Related Articles
- Sliding Window: Prefix sums can accelerate sum calculations over fixed or dynamic windows.
- Two Pointers: Can work together for problems involving sums in ranges.
- Dynamic Programming: Prefix sums are often a foundational step in range-based DP problems.