Skip to content

Commit 09b5b78

Browse files
authored
Merge pull request #58 from Jer3myYu/java-solutions-prefix-sums
Java Chapter 10: Prefix Sums
2 parents 02a0af1 + 88f6f89 commit 09b5b78

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

java/Prefix Sums/KSumSubarrays.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class KSumSubarrays {
2+
public int kSumSubarrays(int[] nums, int k) {
3+
int n = nums.length;
4+
int count = 0;
5+
// Populate the prefix sum array, setting its first element to 0.
6+
int[] prefixSum = new int[nums.length + 1];
7+
for (int i = 0; i < n; i++) {
8+
prefixSum[i+1] = prefixSum[i] + nums[i];
9+
}
10+
// Loop through all valid pairs of prefix sum values to find all
11+
// subarrays that sum to 'k'.
12+
for (int j = 1; j < n + 1; j++) {
13+
for (int i = 1; i < j + 1; i++) {
14+
if (prefixSum[j] - prefixSum[i - 1] == k) {
15+
count++;
16+
}
17+
}
18+
}
19+
return count;
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class KSumSubarraysOptimized {
5+
public int kSumSubarraysOptimized(int[] nums, int k) {
6+
int count = 0;
7+
// Initialize the map with 0 to handle subarrays that sum to 'k'
8+
// from the start of the array.
9+
Map<Integer, Integer> prefixSumMap = new HashMap<>();
10+
prefixSumMap.put(0, 1);
11+
int currPrefixSum = 0;
12+
for (int num : nums) {
13+
// Update the running prefix sum by adding the current number.
14+
currPrefixSum += num;
15+
// If a subarray with sum 'k' exists, increment 'count' by the
16+
// number of times it has been found.
17+
if (prefixSumMap.containsKey(currPrefixSum - k)) {
18+
count += prefixSumMap.get(currPrefixSum - k);
19+
}
20+
// Update the frequency of 'curr_prefix_sum' in the hash map.
21+
int freq = prefixSumMap.getOrDefault(currPrefixSum, 0);
22+
prefixSumMap.put(currPrefixSum, freq + 1);
23+
}
24+
return count;
25+
}
26+
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Arrays;
2+
3+
public class ProductArrayWithoutCurrentElement {
4+
public int[] productArrayWithoutCurrentElement(int[] nums) {
5+
int n = nums.length;
6+
int[] res = new int[n];
7+
Arrays.fill(res, 1);
8+
// Populate the output with the running left product.
9+
for (int i = 1; i < n; i++) {
10+
res[i] = res[i - 1] * nums[i - 1];
11+
}
12+
// Multiply the output with the running right product, from right to
13+
// left.
14+
int rightProduct = 1;
15+
for (int i = n - 1; i > -1; i--) {
16+
res[i] *= rightProduct;
17+
rightProduct *= nums[i];
18+
}
19+
return res;
20+
}
21+
}

java/Prefix Sums/SumBetweenRange.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class SumBetweenRange {
2+
int[] prefixSum;
3+
4+
public SumBetweenRange(int[] nums) {
5+
this.prefixSum = new int[nums.length];
6+
this.prefixSum[0] = nums[0];
7+
for (int i = 1; i < nums.length; i++) {
8+
this.prefixSum[i] = this.prefixSum[i - 1] + nums[i];
9+
}
10+
}
11+
12+
public int sumRange(int i, int j) {
13+
if (i == 0) {
14+
return this.prefixSum[j];
15+
}
16+
return this.prefixSum[j] - this.prefixSum[i - 1];
17+
}
18+
}

0 commit comments

Comments
 (0)