Skip to content

Commit 3ac9e28

Browse files
committed
Two-Hundred-Forty-Seven Commit: Add Maximum Size Subarray Sum Equals k problem to Prefix Sum Section
1 parent 9c85469 commit 3ac9e28

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Prefix_Sum/Problem_Left_And_Right_Sum_Differences.java renamed to src/Prefix_Sum/Problem_2_Left_And_Right_Sum_Differences.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Problem Statement: Left and Right Sum Differences (easy)
44
// LeetCode Question: 2574. Left and Right Sum Differences
55

6-
public class Problem_Left_And_Right_Sum_Differences {
6+
public class Problem_2_Left_And_Right_Sum_Differences {
77
public int[] findDifferenceArray(int[] nums){
88
int n = nums.length;
99
int[] leftSum = new int[n];
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Prefix_Sum;
2+
3+
// Problem Statement: Maximum Size Subarray Sum Equals k (medium)
4+
// LeetCode Question: 325. Maximum Size Subarray Sum Equals k
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class Problem_3_Maximum_Size_SubArray_Sum_Equals_K {
10+
public int maxSubArrayLen(int[] nums, int k){
11+
Map<Integer, Integer> cumMap = new HashMap<>();
12+
int cumSum = 0;
13+
int maxLen = 0;
14+
for (int i = 0; i < nums.length; i++) {
15+
cumSum += nums[i];
16+
if (cumSum == k) {
17+
maxLen = i + 1;
18+
}
19+
if (cumMap.containsKey(cumSum - k)) {
20+
maxLen = Math.max(maxLen, i - cumMap.get(cumSum - k));
21+
}
22+
if (!cumMap.containsKey(cumSum)) {
23+
cumMap.put(cumSum, i);
24+
}
25+
}
26+
return maxLen;
27+
}
28+
}

0 commit comments

Comments
 (0)