Complete the code to initialize the map for prefix sums.
prefixSums := make(map[int]int) prefixSums[[1]] = 1
We start with prefix sum 0 having count 1 because the sum before starting is zero.
Complete the code to update the running sum with the current number.
for _, num := range nums { sum [1]= num // rest of code }
We add the current number to the running sum to keep track of the prefix sum.
Fix the error in checking if (sum - k) exists in the map.
if count, ok := prefixSums[sum [1] k]; ok { result += count }
We check if (sum - k) exists because it means a subarray sums to k.
Fill both blanks to update the map with the current sum count.
prefixSums[sum] = prefixSums[[1]] [2] 1
We increase the count of the current prefix sum by 1 in the map.
Fill all three blanks to complete the function that returns the count of subarrays summing to k.
func subarraySum(nums []int, k int) int {
prefixSums := make(map[int]int)
prefixSums[[1]] = 1
sum := 0
result := 0
for _, num := range nums {
sum [2]= num
if count, ok := prefixSums[sum [3] k]; ok {
result += count
}
prefixSums[sum] = prefixSums[sum] + 1
}
return result
}The function initializes prefixSums with 0 key, adds current num to sum, checks if (sum - k) exists, and updates result accordingly.
