Skip to content

Java Chapter 10: Prefix Sums #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions java/Prefix Sums/KSumSubarrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class KSumSubarrays {
public int kSumSubarrays(int[] nums, int k) {
int n = nums.length;
int count = 0;
// Populate the prefix sum array, setting its first element to 0.
int[] prefixSum = new int[nums.length + 1];
for (int i = 0; i < n; i++) {
prefixSum[i+1] = prefixSum[i] + nums[i];
}
// Loop through all valid pairs of prefix sum values to find all
// subarrays that sum to 'k'.
for (int j = 1; j < n + 1; j++) {
for (int i = 1; i < j + 1; i++) {
if (prefixSum[j] - prefixSum[i - 1] == k) {
count++;
}
}
}
return count;
}
}
27 changes: 27 additions & 0 deletions java/Prefix Sums/KSumSubarraysOptimized.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.HashMap;
import java.util.Map;

public class KSumSubarraysOptimized {
public int kSumSubarraysOptimized(int[] nums, int k) {
int count = 0;
// Initialize the map with 0 to handle subarrays that sum to 'k'
// from the start of the array.
Map<Integer, Integer> prefixSumMap = new HashMap<>();
prefixSumMap.put(0, 1);
int currPrefixSum = 0;
for (int num : nums) {
// Update the running prefix sum by adding the current number.
currPrefixSum += num;
// If a subarray with sum 'k' exists, increment 'count' by the
// number of times it has been found.
if (prefixSumMap.containsKey(currPrefixSum - k)) {
count += prefixSumMap.get(currPrefixSum - k);
}
// Update the frequency of 'curr_prefix_sum' in the hash map.
int freq = prefixSumMap.getOrDefault(currPrefixSum, 0);
prefixSumMap.put(currPrefixSum, freq + 1);
}
return count;
}

}
21 changes: 21 additions & 0 deletions java/Prefix Sums/ProductArrayWithoutCurrentElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.Arrays;

public class ProductArrayWithoutCurrentElement {
public int[] productArrayWithoutCurrentElement(int[] nums) {
int n = nums.length;
int[] res = new int[n];
Arrays.fill(res, 1);
// Populate the output with the running left product.
for (int i = 1; i < n; i++) {
res[i] = res[i - 1] * nums[i - 1];
}
// Multiply the output with the running right product, from right to
// left.
int rightProduct = 1;
for (int i = n - 1; i > -1; i--) {
res[i] *= rightProduct;
rightProduct *= nums[i];
}
return res;
}
}
18 changes: 18 additions & 0 deletions java/Prefix Sums/SumBetweenRange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class SumBetweenRange {
int[] prefixSum;

public SumBetweenRange(int[] nums) {
this.prefixSum = new int[nums.length];
this.prefixSum[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
this.prefixSum[i] = this.prefixSum[i - 1] + nums[i];
}
}

public int sumRange(int i, int j) {
if (i == 0) {
return this.prefixSum[j];
}
return this.prefixSum[j] - this.prefixSum[i - 1];
}
}