Recognize the pattern
When should you carry the past forward?
Look for repeated range totals, contiguous sums, balance points, or any question where many states differ only by a boundary. Prefix Sum replaces repeated traversal with a cumulative state that can be subtracted, compared, or indexed.
Boundary recipe
- 01Choose whether prefix[i] includes or excludes index i.
- 02Seed the empty prefix explicitly.
- 03Express the target as a relationship between boundaries.
- 04Store counts when multiple earlier boundaries matter.