Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 39s1 days 16 hours 1 minutes 39 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 39s1 days 16 hours 1 minutes 39 seconds.

Sign in to watch this lesson
Algorithms - Arrays · 4:00
Counting subarrays that sum to k, even with negative numbers, in one pass: a subarray sum is the difference of two prefixes, so we sweep the prefixes, look back k in a tally of the ones already seen, and only then insert the new one. We trace it on a small array, cover the pitfalls (the starting zero, look-up order, negative remainders in C++), extend it to divisibility, longest subarrays, balanced windows and XOR, and time it against checking every pair on a Mac.