Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA

An array of 6 zeros (indices 0 to 5) receives three range-add operations, each adding v to every index from l to r inclusive: (1,3,+2), (2,5,-1), (0,2,+4). Which final array results?

  1. A. 4 6 6 1 -1 -1
  2. B. 4 6 5 1 0 -1
  3. C. 4 6 5 1 -1 -1
  4. D. 4 2 2 1 -1 -1

Correct answer: C. 4 6 5 1 -1 -1

Use a difference array: add v at l and subtract v at r+1, then take prefix sums. Forgetting the subtraction at r+1 leaves the values inside each range running to the end of the array.

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

Exams that ask this topic