A2SV G5 Remote Education Education Phase I Prefix Sum No Code 1
A2SV G5 Remote Education Education Phase I Prefix Sum No Code 1
A2SV G5 Remote Education Education Phase I Prefix Sum No Code 1
Lecture Flow
● Pre-requisites
● Motivation
● Definition
● Variants
● Common Pitfalls
● Complexity Analysis
● Applications of prefix sum
● Practice Questions
Pre-requisites
1. Arrays and Matrices
2. Familiarity with Arithmetic Operations
3. Understanding Pointers or Indices
Motivation
Given an integer array nums, handle multiple queries of the following type:
1. Calculate the sum of the elements of nums between indices left and right inclusive
where left <= right.
Example:
Input: nums = [-2, 0, 3, -5, 2, -1], queries = [[0, 2], [2, 5], [0, 5]]
1. Running Sum
2. 1D Prefix Sum
3. Inclusive Prefix Sum
4. Exclusive Prefix Sum
5. Range Updates
6. 2D Prefix Sum
Running Sum
● The summation of a sequence of numbers which is updated each time a
new number is added to the sequence
○ done by adding the value of the new number to the previous running
total.
● It allows the total to be stated at any point in time without having to sum
the entire sequence each time
● Used if the particular numbers are not individually important.
Running Sum
Example: consider the sequence <5, 8, 3, 2>
Total = 5 + 8 + 3 + 2
Now insert 6 to the sequence <5, 8, 3, 2, 6>
Total = 5 + 8 + 3 + 2 + 6
Problem Link
1D Prefix Sum
● The 1-dimensional prefix sum algorithm is a simple and efficient
technique for calculating the cumulative sum of a sequence of numbers.
● The basic idea is to iterate over the sequence and compute the sum of
all previous elements for each element in the sequence.
● The resulting array is called the prefix sum array.
1D Prefix Sum
● Here's an example to illustrate the 1-dimensional prefix sum algorithm:
numbers = [3, 1, 7, 0, 4, 1, 6]
Problem Link
Problem Pattern
Given an array of integers nums, calculate the pivot index of this array.
The pivot index is the index where the sum of all the numbers strictly to the left of the index
is equal to the sum of all the numbers strictly to the index's right.
If the index is on the left edge of the array, then the left sum is 0 because there are no
elements to the left. This also applies to the right edge of the array.
Return the leftmost pivot index. If no such index exists, return -1.
Input: nums = [1,7,3,6,5,6]
Output: 3
Explanation: The pivot index is 3.
Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
Right sum = nums[4] + nums[5] = 5 + 6 = 11
Practice
Problem Link
Inclusive Prefix Sum
● Sums up all the elements before and including the current element
Given an array of n elements
A = [a0, a1, a2, a3, . . . , an-1 ]
The inclusive prefix sum will be:
prefix_sum = [a0, a0 + a1, a0 + a1 + a2, . . . , a0 + . . . + an-2 + an-1 ]
Inclusive Prefix Sum
Example:
A = [3, 1, 7, 0, 4, 1, 6, 3]
prefix_sum = [3, 4, 11, 11, 15, 16, 22, 25]
Inclusive Prefix Sum - Implementation
● For a given array called nums:
# identity element
accumulator = 0
for i in range(len(nums)):
accumulator += nums[i]
prefix_sum[i] = accumulator
Exclusive Prefix Sum
● Sums up all the elements before and not including the current element
Given an array of n elements
A = [a0, a1, a2, a3, . . . , an-1 ]
The exclusive prefix sum will be:
prefix_sum = [i, i + a0, a0 + a1, a0 + a1 + a2, . . . , a0 + . . . + an-2 + an-1 ]
Where i is the identity / zero element
Exclusive Prefix Sum
Example:
A = [3, 1, 7, 0, 4, 1, 6, 3]
prefix_sum = [0, 3, 4, 11, 11, 15, 16, 22, 25]
1D range updates using prefix sum
Motivating problem: Array Manipulation
Problem Summary:
● We are given an array and multiple queries asking us to add a constant
value to all elements in the range l to r
1D range updates using prefix sum
● What if we were asked to add a constant value to the suffix of the array
starting at index l, for multiple queries?
Example: given nums = [1, 3, 5, 7] and queries = [[1, 2], [2, 1]]
where queries[i] = [l, k] for: 0 <= i < len(queries),
l = index (0-indexed),
k = constant value
1D range updates using prefix sum
● Instead of updating the whole suffix for each query, we can only
increment the first index of the suffix for each query,
○ prefix_sum = [ 0, 0, 0, 0]
i.e. prefix_sum[l] += k.
For i = 0: prefix_sum = [ 0, 2, 0, 0]
For i = 1: prefix_sum = [ 0, 2, 1, 0]
1D range updates using prefix sum
● Then after all queries, we take the prefix sum of the whole array.
prefix_sum = [0, 2, 3, 3]
Array Manipulation
2D Prefix Sum
● The 2D prefix sum is a technique used to efficiently calculate the sum of
a sub-rectangle in a 2D array.
● The basic idea is to precompute the sum of all elements in the rectangle
with upper left corner (0, 0) and lower right corner (i, j), where i and j are
the row and column indices, respectively.
2D Prefix Sum
● This can be done using the 2D prefix sum array, which stores the
cumulative sum of elements from (0, 0) to (i, j) for all i and j.
● Calculate the sum of the elements of matrix inside the rectangle defined by its upper
left corner (row1, col1) and lower right corner (row2, col2).
● NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.
● int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of
matrix inside the rectangle defined by its upper left corner (row1, col1) and lower
right corner (row2, col2).
You must design an algorithm where sumRegion works on O(1) time complexity.
Practice
Problem Link
Common Pitfalls
There are several pitfalls on prefix sum:
1. Off-by-one Errors
2. Inconsistent Indexing
3. Forgetting to Initialize the Prefix Sum
4. Misunderstanding the Problem Requirements
5. Not Considering Edge Cases
Off-by-one Errors
These errors occur when we incorrectly compute the indices or bounds of
the array, resulting in incorrect or unexpected results.