We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

It took me a while to understand as well, but its pretty brilliant actually.
First let me explain why this approach is good: You have k operations acting on a range of [a,b], for each of these k operations, a could be 1 and b = n , that makes a normal (add sum to 'a' through 'b') solution O(k * n ).

In this solution for each of the k operations, you make exactly 2 operations, so its constant time for each k operation. At the end, you go through the entire n length array and get the largest. That makes it a O(k) + O(n) operation ==> much cheaper

Now, on to the solution itself, assume the array size, n = 10
lets take operations to be(lowerIndex upperIndex sumToBeAdded):
oper1: 1 5 3 oper2: 4 8 7 oper3: 6 9 1

initially your array looks like (array of size n + 1): 00000000000

after 1 5 3 : 0 3 0 0 0 0 -3 0 0 0 0

after 4 8 7 : 0 3 0 0 7 0 -3 0 0 -7 0

after 6 9 1 : 0 3 0 0 7 0 -2 0 0 -7 -1

so, that is O(k) where we processed the array, now for the magic... as we iterate through the n elements, keep adding a running sum, the running sum at each index will give you the final value that was supposed to be at that index (had you added the oper's sum to 'a' through 'b' for each of the k operations...)

on filling all elements of array[n+1] with the running sum and noting the largest, the array transforms to : 0 3 3 3 10 10 8 8 8 1 0 , and this happends in O(n) time

## Array Manipulation

You are viewing a single comment's thread. Return to all comments →

It took me a while to understand as well, but its pretty brilliant actually. First let me explain why this approach is good: You have k operations acting on a range of [a,b], for each of these k operations, a could be 1 and b = n , that makes a normal (add sum to 'a' through 'b') solution O(k * n ).

In this solution for each of the k operations, you make exactly 2 operations, so its constant time for each k operation. At the end, you go through the entire n length array and get the largest. That makes it a O(k) + O(n) operation ==> much cheaper

Now, on to the solution itself, assume the array size, n = 10 lets take operations to be(lowerIndex upperIndex sumToBeAdded): oper1: 1 5 3 oper2: 4 8 7 oper3: 6 9 1

initially your array looks like (array of size n + 1): 00000000000

after 1 5 3 : 0 3 0 0 0 0 -3 0 0 0 0

after 4 8 7 : 0 3 0 0 7 0 -3 0 0 -7 0

after 6 9 1 : 0 3 0 0 7 0 -2 0 0 -7 -1

so, that is O(k) where we processed the array, now for the magic... as we iterate through the n elements, keep adding a running sum, the running sum at each index will give you the final value that was supposed to be at that index (had you added the oper's sum to 'a' through 'b' for each of the k operations...)

on filling all elements of array[n+1] with the running sum and noting the largest, the array transforms to : 0 3 3 3 10 10 8 8 8 1 0 , and this happends in O(n) time

and the largest value at any index is 10

Hope this makes it easier to understand...

Best explanation. Thank you!

Good Explanation

But i have doubt..

consider a logic where you add value to all elements from p to q

after 1 5 3: 0 3 3 3 3 3 0 0 0 0 after 4 8 7: 0 3 3 3 10 10 10 10 10 0

At this step, largest element is 10 But as per your logic largest element is 7

can you explain please?

I understand this works but I didn't get "that makes a normal (add sum to 'a' through 'b') solution O(k * n )."

It would be of immense help If you could help