🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.O(n · q)
  2. B.O(q log n)
  3. C.O(n²)
  4. 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 →