Prefix Sum

Precompute running totals so the sum of any range can be answered instantly instead of adding it up every time.

Related concepts

TopicDescription
Kadane's AlgorithmTrack the best running sum ending at each position to find the maximum sum of a contiguous subarray in one pass.
Hash TablesKey-to-value lookup in average O(1) via hashing into buckets.
Sliding WindowSolve fixed-window problems: maximum sum of k elements, moving averages, and O(n) updates as the window slides.
Two PointersScan a sorted array or string from both ends at once to find pairs, remove duplicates, or reverse data in O(n).
Binary SearchCut a sorted range in half on every step to find a value or boundary in O(log n) instead of checking one by one.
Depth-First SearchExplore as far as possible down one path before backtracking, used to walk trees, graphs, and grids.