Subarray Sum Equals K

Medium

Given nums and k, return the total number of contiguous subarrays whose sum equals k.

Example 1

subarraySum()
Input
nums = [1,1,1], k = 2
Output
2

Example 2

subarraySum()
Input
nums = [1,2,3], k = 3
Output
2

Why

[1,2] and [3]
Constraints
1 <= nums.length <= 2 * 10^4
-1000 <= nums[i] <= 1000
-10^7 <= k <= 10^7

Topics

ArraysHash TablePrefix Sum

Implement subarraySum in the editor and return the answer — printing it is never graded.