How do you count the number of contiguous subarrays whose sum equals a given target?
Keep a running prefix sum and a hash map that records how many times each prefix sum has been seen. For each new element, the sum of any subarray ending here equals the current prefix sum minus some earlier prefix sum, so the number of subarrays ending here with the target sum is exactly the number of times the value current minus target has already appeared.
The seed matters. You start the map with the value zero already present once, because a subarray that starts at index zero has no earlier prefix before it. Without that seed, every subarray that begins at the front of the array would be missed.
The second important detail is the order of the two operations. You look up current minus target before you record the current prefix sum, otherwise a zero target would match the subarray of length zero that you are in the middle of building.
The trade-off to name is that this only works when the array may contain negative numbers, which is exactly why it beats the classic sliding window. A sliding window relies on the sum only moving in one direction as you extend it, and adding a negative number can move it back, so the window has to grow and shrink unpredictably. The prefix sum map has no such assumption and works for any integers, at the cost of linear space. The brute force alternative tries every start and end pair, which is quadratic and easy to write but far too slow on large inputs.