Given an array of size N which is initialized with all zeros. We are given many range add queries, which should be applied to this array. We need to print final updated array as our result.
N = 6 Arr = [0, 0, 0, 0, 0, 0] rangeUpdate1 [0, 2], add 100 Arr = [100, 100, 100, 0, 0, 0] rangeUpdate1 [1, 5], add 100 Arr = [100, 200, 200, 100, 100, 100] rangeUpdate1 [2, 3], add 100 Arr = [100, 200, 300, 200, 100, 100] Which is the final updated array.
This problem can be solved using segment tree with lazy updates in O(log N) time per query but we can do better here, as update operation is not given. We can process each query in constant time using this logic, when a query to add V is given in range [a, b] we will add V to arr[a] and –V to arr[b+1] now if we want to get the actual values of array we will convert the above array into prefix sum array. See below example to understand,
Arr = [0, 0, 0, 0, 0, 0] rangeUpdate1 [0, 2], add 100 Arr = [100, 0, 0, -100, 0, 0] rangeUpdate1 [1, 5], add 100 Arr = [100, 100, 0, -100, 0, 0, -100] rangeUpdate1 [2, 3], add 100 Arr = [100, 100, 100, -100, -100, 0, -100] Now we will convert above operation array to prefix sum array as shown below, Arr = [100, 200, 300, 200, 100, 100] Which is the final updated array.
So in effect, when we add a value V to specific index of array, It represents adding V to all elements right to this index, that is why we add –V after range to remove its effect after its range of add query.
Please note in below code, if range spans till the last index, the addition of –V is omitted to be in memory limit of the array.
100 200 300 200 100 100
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find frequency of each element in a limited range array in less than O(n) time
- Sorted subsequence of size 3 in linear time using constant space
- Kth smallest element in the array using constant space when array can't be modified
- Maximize the first element of the array such that average remains constant
- Find duplicates in constant array with elements 0 to N-1 in O(1) space
- Program to print an array in Pendulum Arrangement with constant space
- Minimizing array sum by applying XOR operation on all elements of the array
- Maximizing array sum with given operation
- Sum of the updated array after performing the given operation
- Make the array non-decreasing with the given operation
- Maximum possible array sum after performing the given operation
- Minimize the non-zero elements in the Array by given operation
- Minimum possible sum of array elements after performing the given operation
- Make all elements of an array equal with the given operation
- Reduce the array to a single integer with the given operation