Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft

A difference array is used for q range-increment updates followed by one final read of all n values. What is the total time?

  1. A. O(q log n)
  2. B. O(n log q)
  3. C. O(nq)
  4. D. O(n + q)

Correct answer: D. O(n + q)

Each update touches two cells of the difference array (O(1)), and one prefix-sum pass of O(n) recovers all values.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic