← Quiz: Prefix Sums and Difference Arrays: all 13 questions
Quiz: Prefix Sums and Difference Arrays · Question 13 of 13
An array never changes and you must answer 10⁵ "sum of a[l..r]" queries. What is the total cost with prefix sums?
An array never changes and you must answer 10⁵ "sum of a[l..r]" queries. What is the total cost with prefix sums?
- A.O(n · q)
- B.O(q log n)
- C.O(n²)
- D.O(n + q)
Answer & explanation
Correct answer: O(n + q)
Build P once in O(n); then each query is a single subtraction.
Can you get the rest right without peeking?
Take the timed Quiz: Prefix Sums and Difference Arrays quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →