Ten problems that drill the prefix-sum pattern from Lesson 7. The big idea keeps coming back: precompute running totals so any range question becomes a subtraction, and store prefix sums in a hash map so “find a subarray with property X” collapses to a lookup. Each solution here was actually run and tested.
How to practise. For each problem, first decide what the prefix sum means (a running total? a running balance of +1/−1? a running sum mod k?) and what you need to store (just the array, or a map of sum → index / count). Try it yourself for ~20 minutes, then read the approach, close the page, and re-solve from memory the next day.
The 10 problems
- 1Range Sum Query - Immutable (LeetCode 303)EasyAnswer many range-sum queries on a fixed array: build a prefix array once, then each query is one subtraction.arrow_forward
- 2Find Pivot Index (LeetCode 724)EasyFind the first index where left sum equals right sum: keep a running left sum, right = total - left - x.arrow_forward
- 3Minimum Value to Get Positive Step by Step Sum (LeetCode 1413)EasySmallest start value keeping a running sum at least 1: find the lowest prefix sum and lift it to exactly 1.arrow_forward
- 4Subarray Sum Equals K (LeetCode 560)MediumCount subarrays summing to k: each is a difference of prefix sums, so count earlier prefixes equal to s − k.arrow_forward
- 5Product of Array Except Self (LeetCode 238)MediumProduct of all other elements at each index without division: multiply a prefix product by a suffix product.arrow_forward
- 6Contiguous Array (LeetCode 525)MediumLongest subarray with equal 0s and 1s: count 0 as -1, then find the farthest-apart repeat of a running sum.arrow_forward
- 7Subarray Sums Divisible by K (LeetCode 974)MediumCount subarrays whose sum is divisible by k: two prefix sums with the same remainder mod k bound one.arrow_forward
- 8Continuous Subarray Sum (LeetCode 523)MediumSubarray of length ≥ 2 summing to a multiple of k? Store each remainder's first index and check the gap.arrow_forward
- 9Range Sum Query 2D - Immutable (LeetCode 304)MediumSum any sub-rectangle of a fixed matrix in O(1): a 2-D prefix sum plus inclusion–exclusion on four corners.arrow_forward
- 10Maximum Size Subarray Sum Equals k (LeetCode 325)MediumLongest subarray summing to k: store each prefix sum's earliest index and look up current - k.arrow_forward