ALG-M005MediumPrefix Sum
Count Target-Sum Subarrays
Count non-empty contiguous subarrays whose sum equals target. Negative numbers and zero are allowed.
Contract
input numbers: integer[]
input target: integer
return integer
Constraints
- • 0 <= length <= 20000
Examples
Example 1
numbers = [1, 1, 1]
target = 2
→ 2
Example 2
numbers = [1, -1, 0]
target = 0
→ 3
5 judge tests (2 shown above). Target complexity: O(n) average.