count
to 0 to store the total count of subarrays.sum_occurrences
with a key-value pair {0: 1}
to handle the case when a subarray starts from index 0.cumulative_sum
to 0 to store the sum of elements up to the current index.nums
.
a. Add the current element to cumulative_sum
.
b. Check if cumulative_sum - k
is present in sum_occurrences
.
i. If it is, add the value of sum_occurrences[cumulative_sum - k]
to count
.
c. Update sum_occurrences
with the current cumulative_sum
.
i. If cumulative_sum
is already a key, increment its value.
ii. If cumulative_sum
is not a key, add it with the value 1.count
.