Prefix & Suffix — definition.
Definition
Precompute cumulative arrays so range queries answer in O(1).
Complexity
Preprocess O(n); query O(1).
When to use it
- Many range sum / product / min queries.
- You need "everything except index i" in linear time.
- Stateless transforms on immutable arrays.
Representative scenarios
- Exclusive Revenue — product of array except self.
- Range Tally — sum over [l, r].
- Running Balance — detect first index where total flips sign.
Related patterns
Read the full pattern page: Prefix & Suffix — template, code, and drills.
Run the free diagnostic.
Ten-minute patterns quiz. No card. Personalized loop starts on the other side.