Find All Subarrays With a Given Sum
Problem Given an integer array (which may contain negative numbers) and a target value, count the number of contiguous subarrays whose elements sum to the target.
Input / Output
- Input: integer array
numsand integertarget. - Output: the count of contiguous subarrays summing to
target.
Constraints
- 1 ≤ n ≤ 10^4.
- Values may be negative, so a sliding window does not apply.
- Subarrays are contiguous; the empty subarray is not counted.
Example
[1,2,3], target = 3→ 2 ([1,2] and [3]).[1,-1,0], target = 0→ 3 — the negatives are exactly why a sliding window fails here.
added …