arraysdsahashing
Count subarrays with prefix sums
Anonymous
To count subarrays with sum k, keep a frequency map of prefix sums, initialized with count 1 for sum 0. At each new prefix p, add the count of p - k, then increment the count for p. This handles negative values too; with expected constant-time hash operations, it takes O(n) time and O(n) space.
0