Concepts

Prefix-sum range queries

Try the free JavaScript compiler

Cumulative totals support half-open interval sums using prefix[r] - prefix[l]. For static bounded numeric arrays, linear preprocessing and constant-query arithmetic.

Formal Syntax & Structure

javascript syntaxjavascript
prefix[0] = 0; prefix[i + 1] = prefix[i] + a[i]

Working Example

Example Snippetjavascript
const values=[3,-2,5,1];const p=[0];for(const x of values)p.push(p[p.length-1]+x);console.log(JSON.stringify(p));
JavaScript Reference