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
- Build S from the right: S[i] is the sum of items from index i to the end.
- Start with S[n−1] = a[n−1] and add one item at a time moving left.
- The sum of any tail is now just S[i]; combined with prefix sums, any range is one lookup.
Watch it run
Loading animation…