Subarray Sum Equals K
Given an array of integers numskk
A subarray is a contiguous non-empty
Example 1
Output: 2
Explanation:
one contiguous subarray is from index 0-1
[1, 1]second contiguous subarray is from index 1-2
[1, 1]Example 2
Output: 2
Example 3
Output: 3
Explanation:
one contiguous subarray at index 0
[1]second contiguous subarray is from index 0-2
[1, -1, 1]third contiguous subarray at index 2
[1]Constraints:
1 <= nums.length <= 2 * 104-1000 <= nums[i] <= 1000-107 <= k <= 107
Solution 1 - Create prefix sum array and then find if there is a possible continuous subarray with K sum
As per the problem statement, one of the key point to remember is that the input array can contain negative numbers, so we have to find continuous subarrays that includes negative numbers too for example if the k=1[1, -1, 1][1][1, -1, 1]1
In this approach, we will first compute a prefix sumHashMapprefix - kresult
The main idea is if we find the prefix -k
Example:
Lets look at an example with input array [1, 2, 3, 4, 5]k = 9[2, 3, 4][4, 5]
-
First, lets compute the prefix sum array using:
So, the prefix sum array will beJavapreffixSum[i] = nums[i] + (i==0 ? 0 : preffixSum[i-1]);[1, 3, 6, 10, 15] -
Next, we will create a
to storeHashMap and store the count as 1, or if it already exists then increment the count.prefix -k-
If we find
in the array, that means the sub array where the index of this value started (exclusive), and until the current prefix it is a contiguous subarray. In the above example, at indexprefix -k in prefix sum we have value3 so10 will beprefix-k and this value is at index1 , so that means there is a sub array from index0 to index1 in the original array. [2, 3, 4]3 -
Similarly, at index at index
in prefix sum we have value4 so15 will beprefix-k and this value is at index6 , so that means there is a sub array from index2 to index3 in the original array. [4, 5]4
Note: Also, store
as key and0 as value, because, if the sum1 starts from index 0. The reason we need to do this is because, if the sumk includes thek index in input array, for example0th , here first element in array is 1, andnums = [1, -1, 1] and k=1 will bek- prefix . So, to account this we will store with 0, there will be always 1 answer.0 -
If we find
import java.util.HashMap;
public class SubarraysSumEqualToK {
static int subarraySum(int[] nums, int k) {
int result = 0;
int[] prefixSum = new int[nums.length];
prefixSum[0] = nums[0];
for(int i=1; i<nums.length; i++) {
prefixSum[i] = nums[i] + prefixSum[i-1];
}
HashMap<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
for(int prefix: prefixSum) {
if(map.containsKey(prefix-k)) {
result += map.get(prefix-k);
}
map.merge(prefix, 1, Integer::sum);
}
System.out.println(map);
return result;
}
public static void main(String[] args) {
System.out.println(subarraySum(new int[]{1,-1,1}, 1));
System.out.println(subarraySum(new int[]{1,2,3,4,5}, 9));
}
}
Complexity Analysis:
Time complexity: Above code runs in O(n) time where nnums
Space complexity: O(n), since we are creating prefix sum array with size n
Above implementations source code can be found at