Back to exercises

Subarray Sum Equals K

Keep a running prefix sum and a hash map of how many times each prefix sum has appeared. For each new prefix, look for prefix minus k to count matching earlier starts.

Coding Exercises Medium O(n)

Subarray Sum Equals K

Keep a running prefix sum and a hash map of how many times each prefix sum has appeared. For each new prefix, look for prefix minus k to count matching earlier starts.

Count how many contiguous subarrays sum to k.