Algorithms and Data Structures · Middle · Question 56 of 75

How do you answer "sum of elements from i to j" many times on a static array?

Explanation

Preprocessing is O(n) and every query is O(1). If the array also changes between queries, prefix sums break and you need a Fenwick or segment tree.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 56

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz