Data Structures19 sections · 729 units
Open in Course

The Range Sum Problem

A common bottleneck

Here's a problem you'll see constantly: given an array, answer many queries asking for the sum of elements from index ll to rr.

The naive approach loops from ll to rr and adds up elements. Each query takes O(n)O(n) time. With qq queries, that's O(nq)O(nq) total.

If n=100,000n = 100{,}000 and q=100,000q = 100{,}000, you're doing 1010 billion operations. That's too slow. There's a better way.