Data Structures & AlgorithmsHardGATE CS · DSA

An array of 8 zeros (indices 0–7) receives range updates with a difference array (diff[l] += v, diff[r + 1] −= v, then prefix-sum): add 5 to [2..7]; add 6 to [2..6]; add 6 to [0..5]; add 4 to [3..7]. What is the final array?

  1. A. [6, 6, 17, 21, 21, 15, 9, 0]
  2. B. [6, 6, 17, 21, 21, 21, 15, 9]
  3. C. [6, 6, 9, 15, 17, 21, 21, 21]
  4. D. [7, 6, 17, 21, 21, 21, 15, 9]

Correct answer: B. [6, 6, 17, 21, 21, 21, 15, 9]

Mark +v at l and −v at r + 1 (not at r, which would drop the last index), then take the running sum: [6, 6, 17, 21, 21, 21, 15, 9].

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

Exams that ask this topic