MMeta·DSAE4Technical Phone Screen
Subarray Sum Equals K
Problem
Given an integer array nums and integer k, return the total number of contiguous subarrays whose sum equals k.
Example
nums = [1,1,1], k = 2
Output: 2
Constraints
- 1 ≤ nums.length ≤ 2 × 10^4
- -1000 ≤ nums[i] ≤ 1000
Approach
Prefix-sum + hash map of counts. Note negatives rule out the sliding window.
added 6 days ago