Build a prefix sum as you scan. At each position, the prefix sum P represents the sum of all elements from the start up to the current index.
If some earlier prefix sum was P - k, then the subarray between that position and the current position sums to exactly k. Why? Because P - (P - k) = k.
Use a hash map to count how many times each prefix sum has occurred. When you see prefix sum P, look up how many times P - k appeared. Each occurrence marks a valid subarray ending at the current position.
Initialize the map with {0: 1} to handle subarrays starting from index 0.