Count Subarrays Summing to k

medium
Unit 20 · Data Structures0/3 to milestone

Count the contiguous subarrays whose sum equals k. Values can be negative, so a sliding window doesn't work. Solve it in O(n) with prefix sums and a hash map of how often each prefix sum has occurred.

#prefix-sums#hash-map2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Count Subarrays Summing to k · TechGradify