7 routes42 sections324 units

Unit 1.8 · about 4 h

95 / 324

Prefix sums, difference arrays and prefix plus hash

Precomputation trades linear space for constant queries, and the prefix-plus-hash reduction is the template for turning a range condition into a lookup across counting problems.

  • implement

You can now

  • Build prefix and difference arrays and answer range queries in \(O(1)\).
  • Reduce subarray counting to prefix-frequency lookup with the seed-first rule.
  • Choose windows versus prefix-plus-hash from the sign of the numbers.

Covers

  • Prefix sums, difference arrays and prefix plus hash

Study, in this order

2 steps

  1. Prefix sums and difference arrays
    DSA Handbook - Foundations and linear patterns

    The pattern (build \(\mathrm{prefix}[n+1]\) once, query in \(O(1)\)); Worked example LC 303; What it costs (1D and 2D builds and queries, difference-array updates); 2D prefix sums: inclusion-exclusion on four corners; Difference arrays: the inverse direction

  2. The prefix-sum + hash-map combo
    DSA Handbook - Foundations and linear patterns

    Why sliding window fails on negatives, in one trace ([1, -1, 1, 1]); The reduction (\(\mathrm{prefix}[l] = \mathrm{prefix}[r+1] - k\)); The pattern with the {0: 1} seed; Worked example LC 560 on [1, 1, 1] with \(k = 2\); When the pattern bends (modulo LC 974, first-seen LC 525)

Focus on

  • The prefix array has \(n+1\) entries with \(P[0] = 0\): a range sum is \(P[r+1]-P[l]\), with no special case.
  • Counting needs the seed {0: 1}: subarrays starting at index 0 would otherwise be missed.
  • Difference arrays mark \(+v\) at \(l\) and \(-v\) at \(r+1\); one prefix pass materializes the array, and a second prefix over it answers queries.
  • Python's % already normalizes negatives for positive \(k\); the ((x%k)+k)%k form is for C++ and Java ports.

Reproduce closed-book

By hand on [1, 1, 1] with \(k = 2\): seed {0: 1}; running prefixes 1, 2, 3 need -1, 0, 1 with counts 0, 1, 1, totaling 2. Then implement range-add via difference marks plus range-sum queries, differential-tested against naive loops (\(n\) up to 20, up to 10 updates, derandomize) in Python 3.13: expect 1 passed.

Traps

  • Off-by-one: \(P\) has \(n+1\) entries and the query is \(P[r+1]-P[l]\).
  • Missing seed {0: 1}: every valid prefix starting at index 0 vanishes.
  • Using a window on possibly-negative inputs; reach for prefix-plus-hash instead.

Practice

LeetCode 303: Range Sum Query - Immutable

Build the \(n+1\) prefix once and answer each sumRange in \(O(1)\). Judge with the Example array [-2, 0, 3, -5, 2, -1].

After submitting, compare against the handbook prefix-sums worked example LC 303.

LeetCode 560: Subarray Sum Equals K

Implement the prefix-frequency reduction with the {0: 1} seed. Judge with the Examples [1, 1, 1] \(k = 2\) giving 2, and [1, 2, 3] \(k = 3\) giving 2.

After submitting, compare your trace against the handbook prefix-sum-hash worked example on [1, 1, 1].

Self-test

  1. Derive the 2D range-sum formula.

  2. Why does the difference marking work?

  3. Where does the {0: 1} seed enter the LC 560 trace?

  4. Modulo keys with negative running sums in Python?

Completion criterion

Stop when

Both are accepted, your hand trace totals 2, and you can state the window-versus-prefix choice rule from the sign of the input.

Mark passed, then open 2.1 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • DSA Handbook - Foundations and linear patterns
    Handbook Academyprimarycourse
    Study Part 0, Part 1, Part 3 and Part 4 in order, plus the Part 5 Linked-list fundamentals chapter. Use the routing-rule pages for arrays versus lists, map choice, stack versus queue, two pointers, sliding windows and prefix methods before seeing solved code.

Also in this section

  • AlgoMaster DSA - linear structures slice
    AlgoMasterrequiredpractice
    Attempt Design HashMap, Longest Substring Without Repeating Characters and Sliding Window Maximum cold. Hide tags, state the invariant first, and add randomized operation histories to Design HashMap.
  • MIT 6.042J - asymptotics and invariants
    MIT OpenCourseWaresupporttheory
    Use the asymptotics, state-machine invariants and proof-method readings when a cost or correctness argument is weak. Solve only the matching problems.
  • Algorithms lecture notes - amortized analysis and hashing
    Jeff Ericksonsupporttheory
    Use note 9 Amortized Analysis for the summation, taxation, charging and potential-function proofs, and note 5 Hashing for chaining, universal hashing and open-addressing analysis, when the handbook treatment is too brief for a proof.
  • Algorithmica - memory and data layout
    Sergey Slotinsupportapplication
    Read the memory hierarchy, cache and complexity-model chapters, then measure one contiguous-versus-linked and one hash-versus-ordered workload crossover.
  • NeetCode - unseen linear problems
    NeetCodeoptionalassessment
    Use only for two tag-hidden transfer problems after the AlgoMaster slice. Do not start another full roadmap or watch a solution before the attempt is complete.
  • Hypothesis documentation - Quickstart and stateful testing
    Hypothesissupportreference
    Follow Quickstart for @given strategies and the pytest combination, Configuring test settings for max_examples and derandomize, then Rule-based state machines (rule, precondition, invariant, run_state_machine_as_test) for randomized operation histories against dict and deque oracles.
  • CPython source - list and dict objects
    Python Software Foundationreferencereference
    Read list_resize in Objects/listobject.c for the growth formula and pattern comment, and the USABLE_FRACTION definition, perturbation-probing comment and dummy-entry handling in Objects/dictobject.c. Read the cited functions and comments only.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels