Count the subarrays that sum to k in one pass: keep a running sum and a map of the running sums seen so far, and look up sum − k at each step. O(n) against O(n²) for every pair of ends, and negative values are fine.
This interactive visualization needs JavaScript.