Data Structures19 sections · 729 units
Open in Course

Lessons from Subarray Sum

Combined power

What did you learn?

1.1. Prefix sum + hash map is a powerful combination for subarray problems with exact sum constraints.

2.2. Initialize the hash map with {0:1}\{0: 1\} to handle subarrays starting at index 00.

3.3. This pattern works because you're doing "complement lookup" on prefix sums, just like two-sum on values. This technique solves many variations: subarrays divisible by kk, subarrays with equal 00s and 11s, and more.