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.
Each operation has to be applied to a range of values, i.e., to the values in array having indices starting from p to q. Here, in the first loop, the value to be added (sum variable) is added to the starting location p in the array, and the sum is subtracted from the ending location + 1, i.e., q + 1. In the second loop, the value added to the starting location gets added to successive locations, i.e., it gets added to p, p+1, p+2, .... until q, because each value that was added in the starting location using first loop, will be added to the variable x and therefore in effect gets added to subequent locations. But once the range ends, i.e., once you reach q+1, the sum that was added at starting location will be subtracted. This will subtract the sum value from the variable x in which the sum value was added at starting location p, meaning the addition of sum will only affect locations p to q.
Example:
If n=5 and operations are:-
1 2 100
2 5 100
3 4 100
Initial state of array (starting from index 1 until 5):-
0 0 0 0 0
Applying first operation
Add 100 (sum) at index 1 (p)
Subtract 100 (sum) from index 3 (q+1)
Result
100 0 -100 0 0
Applying second operation
- Add 100 (sum) at index 2 (p)
- Do NOT subtract 100 (sum) from index 6 (q+1) since 6 is not lesser or equal to N (refer to code)
Result
100 100 -100 0 0
Applying third operation
- Add 100 (sum) at index 3 (p)
- Subtract 100 (sum) from index 5 (q+1)
Result
100 100 0 0 -100
If you start adding values from this array to a variable x, the point at which x is maximum is the answer because that is the point which will have the maximum value after applying the operations.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
Explanation of the code:
Each operation has to be applied to a range of values, i.e., to the values in array having indices starting from p to q. Here, in the first loop, the value to be added (sum variable) is added to the starting location p in the array, and the sum is subtracted from the ending location + 1, i.e., q + 1. In the second loop, the value added to the starting location gets added to successive locations, i.e., it gets added to p, p+1, p+2, .... until q, because each value that was added in the starting location using first loop, will be added to the variable x and therefore in effect gets added to subequent locations. But once the range ends, i.e., once you reach q+1, the sum that was added at starting location will be subtracted. This will subtract the sum value from the variable x in which the sum value was added at starting location p, meaning the addition of sum will only affect locations p to q.
Example:
If n=5 and operations are:-
Initial state of array (starting from index 1 until 5):-
Applying first operation
Result
Applying second operation - Add 100 (sum) at index 2 (p) - Do NOT subtract 100 (sum) from index 6 (q+1) since 6 is not lesser or equal to N (refer to code)
Result
Applying third operation - Add 100 (sum) at index 3 (p) - Subtract 100 (sum) from index 5 (q+1)
Result
If you start adding values from this array to a variable x, the point at which x is maximum is the answer because that is the point which will have the maximum value after applying the operations.