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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- LCA for n-ary Tree | Constant Query O(1)
- Array range queries over range queries
- Binary Indexed Tree : Range Update and Range Queries
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Print modified array after multiple array range increment operations
- Find the Initial Array from given array after range sum queries
- Minimum time to return array to its original state after given modifications
- Min-Max Range Queries in Array
- Range Queries for Frequencies of array elements
- Queries for GCD of all numbers of an array except elements in a given range
- Queries for counts of array elements with values in given range
- Binary array after M range toggle operations
- Mean of range in array
- Range Query on array whose each element is XOR of index value and previous element
- Difference Array | Range update query in O(1)
- Array range queries for elements with frequency same as value
- Array range queries for searching an element
- Number whose sum of XOR with given array range is maximum
- Range product queries in an array