Core Idea
A subarray sum from l to r can be written as prefix[r] - prefix[l - 1]. For each ending position, count how many earlier prefix sums would make the difference equal to k.
Algorithm
Scan left to right with a frequency map of prefix sums seen so far. Before inserting the current prefix sum, add the frequency of currentPrefix - k to the answer.
Common Mistakes
Negative numbers mean two pointers do not work here. Count the empty prefix sum 0 before the scan so subarrays starting at position 1 are included.