Skip to content

Commit dfbb97e

Browse files
committed
Two-Hundred-Seventy-Six Commit: Amend Prefix_Sum_Tutorial.txt in Prefix Sum section
1 parent c7fec17 commit dfbb97e

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

src/Prefix_Sum/Prefix_Sum_Tutorial.txt

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
Prefix Sum
22
==========
33

4-
- Introduction
4+
- Introduction:
5+
===============
56

67
The prefix sum, also known as the cumulative sum, is a technique commonly used in computer science and mathematics
78
to preprocess a list of numbers in order to quickly calculate the sum of elements in any subarray. The idea is to
89
create a new array where each element at index `i` is the sum of the elements from the start of the original
910
array up to index `i`.
1011

11-
1. Definition
12+
1. Definition:
13+
--------------
1214

1315
Given an array ( arr ) of length ( n ), the prefix sum array ( prefix_sum ) is defined as:
1416
prefix_sum[i] = arr[0] + arr[1] + ... + arr[i]
1517

1618

17-
2. Steps to Implement the Prefix Sum Pattern
19+
2. Steps to Implement the Prefix Sum Pattern:
20+
---------------------------------------------
1821

1922
1. Compute the Prefix Sum Array:
2023
- 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
2326
2. Use the Prefix Sum Array for Fast Range Sum Queries:
2427
- Once the prefix sum array is constructed, the sum of any subarray `arr[i:j]` can be computed quickly.
2528

26-
3. Prefix Sum Array Calculation
29+
3. Prefix Sum Array Calculation:
30+
--------------------------------
2731

2832
The prefix sum array can be constructed with the following steps:
2933
- Initialize an array `prefixSum` of the same length as `arr`.
3034
- Set the first element of `prefixSum` to be the same as the first element of `arr`.
3135
- For each subsequent element, compute the prefix sum by adding the current element of `arr` to the previous
3236
element of `prefixSum`.
3337

34-
4. Example
38+
4. Example:
39+
-----------
3540

3641
Let's consider an array `arr = [1, 2, 3, 4, 5]`.
3742

@@ -51,7 +56,8 @@ Prefix Sum
5156
- If `i` is 0, the sum is simply `prefixSum[j]`.
5257
- If `i` is greater than 0, the sum is `prefixSum[j] - prefixSum[i-1]`.
5358

54-
5. Example Code in Java
59+
5. Example Code in Java:
60+
------------------------
5561

5662
Here's a Java implementation of the prefix sum pattern:
5763

@@ -92,15 +98,17 @@ Prefix Sum
9298
}
9399
}
94100

95-
6. Explanation
101+
6. Explanation:
102+
---------------
96103

97104
- `computePrefixSum`: This method calculates the prefix sum array.
98105
- `rangeSum`: This method uses the prefix sum array to quickly calculate the sum of elements in the subarray
99106
from index `i` to `j`.
100107
- In the `main` method, we demonstrate how to use these functions to compute the sum of elements in a specific
101108
range.
102109

103-
7. Applications
110+
7. Applications:
111+
----------------
104112

105113
1. Range Sum Queries: After computing the prefix sum array, the sum of any subarray arr[i:j]arr[i:j] can be
106114
calculated in constant time O(1)O(1) as:
@@ -118,12 +126,14 @@ Prefix Sum
118126
sum queries efficiently.
119127

120128
- Prefix Sum Example LeetCode Question: 325. Maximum Size Subarray Sum Equals k
129+
=================================================================================
121130

122131
To find the maximum length of a subarray that sums to a target value `k` in a given array `nums`, you can use a
123132
hashmap to keep track of the cumulative sum at each index. This allows you to efficiently find subarrays that sum to
124133
the target value (ChatGPT coded the solution 🤖).
125134

126135
1. Here's a step-by-step approach:
136+
----------------------------------
127137

128138
1. Initialize a hashmap to store the cumulative sum and its corresponding index.
129139
2. Iterate through the array while maintaining a cumulative sum.
@@ -132,6 +142,7 @@ Prefix Sum
132142
5. Store the cumulative sum in the hashmap if it hasn't been stored before, to maintain the earliest index where this sum occurs.
133143

134144
2. Here is the Java implementation of the algorithm:
145+
----------------------------------------------------
135146

136147
import java.util.HashMap;
137148

@@ -180,18 +191,21 @@ Prefix Sum
180191
}
181192

182193
3. Explanation:
194+
----------------
195+
183196
- sumIndexMap: A hashmap that stores cumulative sums and their earliest indices.
184197
- cumulativeSum: A variable that keeps track of the cumulative sum as we iterate through the array.
185198
- maxLength: Tracks the maximum length of a subarray that sums to \( k \).
186199

187200
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.
195209

196210
This approach efficiently finds the maximum length of a subarray summing to `k` with a time complexity of O(n),
197211
where (n) is the length of the input array.

0 commit comments

Comments
 (0)