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 firstname.lastname@example.org. 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.
- 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
- 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
- Make all elements of an array equal with the given operation
- Minimum possible sum of array elements after performing the given operation
- Reduce the array to a single integer with the given operation
- Minimum cost to equal all elements of array using two operation
- Check if the last element of array is even or odd after performing a operation p times
- Minimum operation to make all elements equal in array
- Minimum number of subtract operation to make an array decreasing
- Find the number of different numbers in the array after applying the given operation q times
- Range and Coefficient of range of Array