Module M16 · 16 of 35 in teaching order · Intermediate

Prefix Sum and Difference Array

Precompute once, answer range questions in O(1); apply range updates in O(1).

Lessons

  1. Precompute running totals once to answer any range sum in O(1), and record range updates in O(1) with a difference array applied in one pass.

Topics this module covers

The full module scope. Topics without a lesson above are not written yet.

  • build
  • range queries
  • prefix sum with hashing
  • prefix products
  • 2D prefix
  • difference arrays
  • range updates

Builds toward