Prefix Sums & Counting
Use for range sums, balance, “# of subarrays with target”.
What this pattern is
Precompute cumulative sums to answer range queries in O(1), or use prefix-sum frequencies to count subarrays matching a target in O(n).
Exercise: rangeSum
Your Solution
Exercise: subarraySum
Task: Count the number of contiguous subarrays whose sum equals k. Use a map of prefixSum→count.
Example: nums=[1,1,1], k=2 → 2; nums=[1,2,3], k=3 → 2
Edge cases: zeros, negative numbers, large arrays.