1
1
Prefix Sum
2
2
==========
3
3
4
- - Introduction
4
+ - Introduction:
5
+ ===============
5
6
6
7
The prefix sum, also known as the cumulative sum, is a technique commonly used in computer science and mathematics
7
8
to preprocess a list of numbers in order to quickly calculate the sum of elements in any subarray. The idea is to
8
9
create a new array where each element at index `i` is the sum of the elements from the start of the original
9
10
array up to index `i`.
10
11
11
- 1. Definition
12
+ 1. Definition:
13
+ --------------
12
14
13
15
Given an array ( arr ) of length ( n ), the prefix sum array ( prefix_sum ) is defined as:
14
16
prefix_sum[i] = arr[0] + arr[1] + ... + arr[i]
15
17
16
18
17
- 2. Steps to Implement the Prefix Sum Pattern
19
+ 2. Steps to Implement the Prefix Sum Pattern:
20
+ ---------------------------------------------
18
21
19
22
1. Compute the Prefix Sum Array:
20
23
- Given an array `arr` of length `n`, create a prefix sum array `prefixSum` where each element at index `i` is
@@ -23,15 +26,17 @@ Prefix Sum
23
26
2. Use the Prefix Sum Array for Fast Range Sum Queries:
24
27
- Once the prefix sum array is constructed, the sum of any subarray `arr[i:j]` can be computed quickly.
25
28
26
- 3. Prefix Sum Array Calculation
29
+ 3. Prefix Sum Array Calculation:
30
+ --------------------------------
27
31
28
32
The prefix sum array can be constructed with the following steps:
29
33
- Initialize an array `prefixSum` of the same length as `arr`.
30
34
- Set the first element of `prefixSum` to be the same as the first element of `arr`.
31
35
- For each subsequent element, compute the prefix sum by adding the current element of `arr` to the previous
32
36
element of `prefixSum`.
33
37
34
- 4. Example
38
+ 4. Example:
39
+ -----------
35
40
36
41
Let's consider an array `arr = [1, 2, 3, 4, 5]`.
37
42
@@ -51,7 +56,8 @@ Prefix Sum
51
56
- If `i` is 0, the sum is simply `prefixSum[j]`.
52
57
- If `i` is greater than 0, the sum is `prefixSum[j] - prefixSum[i-1]`.
53
58
54
- 5. Example Code in Java
59
+ 5. Example Code in Java:
60
+ ------------------------
55
61
56
62
Here's a Java implementation of the prefix sum pattern:
57
63
@@ -92,15 +98,17 @@ Prefix Sum
92
98
}
93
99
}
94
100
95
- 6. Explanation
101
+ 6. Explanation:
102
+ ---------------
96
103
97
104
- `computePrefixSum`: This method calculates the prefix sum array.
98
105
- `rangeSum`: This method uses the prefix sum array to quickly calculate the sum of elements in the subarray
99
106
from index `i` to `j`.
100
107
- In the `main` method, we demonstrate how to use these functions to compute the sum of elements in a specific
101
108
range.
102
109
103
- 7. Applications
110
+ 7. Applications:
111
+ ----------------
104
112
105
113
1. Range Sum Queries: After computing the prefix sum array, the sum of any subarray arr[i:j]arr[i:j] can be
106
114
calculated in constant time O(1)O(1) as:
@@ -118,12 +126,14 @@ Prefix Sum
118
126
sum queries efficiently.
119
127
120
128
- Prefix Sum Example LeetCode Question: 325. Maximum Size Subarray Sum Equals k
129
+ =================================================================================
121
130
122
131
To find the maximum length of a subarray that sums to a target value `k` in a given array `nums`, you can use a
123
132
hashmap to keep track of the cumulative sum at each index. This allows you to efficiently find subarrays that sum to
124
133
the target value (ChatGPT coded the solution 🤖).
125
134
126
135
1. Here's a step-by-step approach:
136
+ ----------------------------------
127
137
128
138
1. Initialize a hashmap to store the cumulative sum and its corresponding index.
129
139
2. Iterate through the array while maintaining a cumulative sum.
@@ -132,6 +142,7 @@ Prefix Sum
132
142
5. Store the cumulative sum in the hashmap if it hasn't been stored before, to maintain the earliest index where this sum occurs.
133
143
134
144
2. Here is the Java implementation of the algorithm:
145
+ ----------------------------------------------------
135
146
136
147
import java.util.HashMap;
137
148
@@ -180,18 +191,21 @@ Prefix Sum
180
191
}
181
192
182
193
3. Explanation:
194
+ ----------------
195
+
183
196
- sumIndexMap: A hashmap that stores cumulative sums and their earliest indices.
184
197
- cumulativeSum: A variable that keeps track of the cumulative sum as we iterate through the array.
185
198
- maxLength: Tracks the maximum length of a subarray that sums to \( k \).
186
199
187
200
4. How It Works:
188
- 1. Initialization: The hashmap is initialized with a cumulative sum of 0 at index -1 to handle the case where the
189
- subarray starts from index 0.
190
- 2. Iteration and Updates: As you iterate through the array, you update the cumulative sum and check
191
- if `cumulativeSum - k` exists in the hashmap. If it does, it means a subarray summing to \( k \) is found.
192
- You then update the `maxLength` if the current subarray length is greater.
193
- 3. Storing Cumulative Sum: Only store the cumulative sum in the hashmap if it hasn't been stored before, ensuring
194
- the earliest index is recorded.
201
+ ----------------
202
+ 1. Initialization: The hashmap is initialized with a cumulative sum of 0 at index -1 to handle the case where the
203
+ subarray starts from index 0.
204
+ 2. Iteration and Updates: As you iterate through the array, you update the cumulative sum and check
205
+ if `cumulativeSum - k` exists in the hashmap. If it does, it means a subarray summing to `k` is found.
206
+ You then update the `maxLength` if the current subarray length is greater.
207
+ 3. Storing Cumulative Sum: Only store the cumulative sum in the hashmap if it hasn't been stored before, ensuring
208
+ the earliest index is recorded.
195
209
196
210
This approach efficiently finds the maximum length of a subarray summing to `k` with a time complexity of O(n),
197
211
where (n) is the length of the input array.
0 commit comments