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?
- A. 4 6 6 1 -1 -1
- B. 4 6 5 1 0 -1
- C. 4 6 5 1 -1 -1
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- The stock span of day i is the number of consecutive days up to and including i with price at most price[i]. What is the sum of the spans…
- For the circular array [1, 2, 1], the next greater element of each position searches forward and wraps around to the start, with -1 when…
- What is the sum of the minimum of every contiguous window of size 4 in [8, 2, 6, 1, 9, 3, 7]?
- A jump game on [2, 3, 0, 1, 4, 0, 2, 1, 3] starts at index 0; from index i you may jump 1 to a[i] steps forward. What is the minimum…
- Balloons are intervals [1,6], [2,8], [7,12], [10,16] on a line. Arrows shot at integer points burst every balloon whose interval contains…
- Around a circular route, station i has gas[i] fuel and the trip to station i+1 costs cost[i]. With gas = [1, 2, 3, 4, 5] and cost = [3, 4…