Prefix Sum Array in DSA Go - Time & Space Complexity
We want to understand how fast we can build and use a prefix sum array.
How does the time needed grow as the input array gets bigger?
Analyze the time complexity of the following code snippet.
package main
func prefixSum(arr []int) []int {
n := len(arr)
prefix := make([]int, n)
prefix[0] = arr[0]
for i := 1; i < n; i++ {
prefix[i] = prefix[i-1] + arr[i]
}
return prefix
}
This code creates a prefix sum array where each element is the sum of all elements before it in the input array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single for-loop that adds elements one by one.
- How many times: The loop runs once for each element after the first, so n-1 times.
As the input array grows, the number of additions grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 9 additions |
| 100 | 99 additions |
| 1000 | 999 additions |
Pattern observation: The operations increase directly with input size, so doubling input roughly doubles work.
Time Complexity: O(n)
This means the time to build the prefix sum grows in a straight line with the size of the input array.
[X] Wrong: "Building a prefix sum array takes more time than just reading the array once because it does extra additions."
[OK] Correct: The prefix sum only adds one extra operation per element, so it still grows linearly and is very efficient.
Knowing how prefix sums work and their time cost helps you solve many problems quickly, showing you understand efficient data processing.
"What if we wanted to build a 2D prefix sum array for a matrix? How would the time complexity change?"
Practice
prefix sum array in algorithms?Solution
Step 1: Understand prefix sum array concept
A prefix sum array stores cumulative sums of elements from the start to each position.Step 2: Use prefix sums for subarray sums
Using prefix sums, sum of any subarray can be found quickly by subtracting two prefix sums.Final Answer:
To quickly find the sum of elements in any subarray -> Option DQuick Check:
Prefix sum array helps fast subarray sums [OK]
- Confusing prefix sum with sorting
- Thinking prefix sum reverses array
- Assuming prefix sum finds max element
prefix from an integer slice arr?Solution
Step 1: Check prefix array size and indexing
Prefix sum array usually has length one more than original to handle zero prefix sum at start.Step 2: Verify loop and indexing logic
Loop from 1 to len(arr), prefix[i] = prefix[i-1] + arr[i-1] correctly accumulates sums.Final Answer:
prefix := make([]int, len(arr)+1) for i := 1; i <= len(arr); i++ { prefix[i] = prefix[i-1] + arr[i-1] } -> Option BQuick Check:
Correct prefix sum initialization uses offset indexing [OK]
- Using prefix array same length as original
- Starting loop at 0 and accessing prefix[-1]
- Incorrect indexing of arr inside loop
arr := []int{2, 4, 6, 8}
prefix := make([]int, len(arr)+1)
for i := 1; i <= len(arr); i++ {
prefix[i] = prefix[i-1] + arr[i-1]
}
fmt.Println(prefix)Solution
Step 1: Calculate prefix sums step-by-step
prefix[0]=0
prefix[1]=0+2=2
prefix[2]=2+4=6
prefix[3]=6+6=12
prefix[4]=12+8=20Step 2: Write final prefix array
The prefix array is [0, 2, 6, 12, 20]Final Answer:
[0 2 6 12 20] -> Option AQuick Check:
Sum accumulates correctly in prefix array [OK]
- Forgetting initial zero in prefix array
- Adding wrong elements at each step
- Confusing prefix array length
arr := []int{1, 3, 5}
prefix := make([]int, len(arr))
for i := 1; i < len(arr); i++ {
prefix[i] = prefix[i-1] + arr[i]
}
fmt.Println(prefix)Solution
Step 1: Check prefix array initialization
Prefix array is size len(arr), which can work but prefix[0] must be set before use.Step 2: Analyze loop and prefix[0]
Loop starts at i=1, prefix[1] = prefix[0] + arr[1]. prefix[0] is zero by default in Go, but it should be explicitly set to arr[0] before the loop.Step 3: Check correctness of prefix sums
prefix[0] is zero, but arr[0] is never added, so prefix sums are incomplete.Final Answer:
prefix[0] is not initialized before use -> Option CQuick Check:
Missing prefix[0] initialization causes wrong sums [OK]
- Not initializing prefix[0]
- Wrong prefix array size
- Incorrect loop start index
arr, how can you use a prefix sum array to find the sum of elements between indices l and r (inclusive) efficiently?Solution
Step 1: Understand prefix sum indexing
Prefix sum array usually has length len(arr)+1 with prefix[0]=0, prefix[i] = sum of arr[0..i-1].Step 2: Calculate subarray sum using prefix sums
Sum from l to r = prefix[r+1] - prefix[l] because prefix[r+1] sums up to arr[r].Final Answer:
sum = prefix[r+1] - prefix[l] -> Option AQuick Check:
Subarray sum = prefix[r+1] - prefix[l] [OK]
- Using prefix[r] - prefix[l-1] without offset
- Adding prefix sums instead of subtracting
- Confusing indices by 1
Ready to prove it?
Pass the test and this pattern moves unknown
