Skip to content
StackDecode
Array techniquesAnimated

Suffix Sum

Running totals from the right end.

Time

O(n) build, O(1) query

Extra space

O(n)

Use it for

Sums of tails; pairs with prefix sums.

How it works

  1. Build S from the right: S[i] is the sum of items from index i to the end.
  2. Start with S[n−1] = a[n−1] and add one item at a time moving left.
  3. The sum of any tail is now just S[i]; combined with prefix sums, any range is one lookup.

Watch it run

Loading animation…