All lessons

Prefix Sums

6 min · C# examples

Precompute running totals so any range sum becomes a quick subtraction.

C# · Prefix Sums0/5 cleared

Enemy 1 of 5

A prefix sum at index i stores the total of everything from the start up to i. Build it once, then the sum of any slice [L..R] is prefix[R] - prefix[L-1].

This turns repeated range-sum questions from O(n) each into O(1) each after O(n) setup.

You
Bug Slime
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.

Stuck? Ask Rena

Your AI tutor that gives assistance.

Asking Rena in lessons is on Ultimate.