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.
Disclaimer: Please correct me if i am wrong, I am just trying to provide a different explanation in the way that it clicked into my head while viewing your solution.
First Loop, Pt 1
I am first going to start off with why the indexes are chosen the way they are. This will lead into the next part of the same loop.
Ok, the first part is array[a - 1], Because the "indexes" given to us in the problem are actually the index + 1,which is something calles 1-indexed, which means the array its based off starts at 1, to stay true to the inclusive rule told to us in the problem, we must start at the beginning index.
We stop at array[b-1] beacuse, like the lat one, the var b given is not a accurate index so we must subtract one from it to get the starting point. To describe when to stop the set (more on this in a second), we need to put the -k one past the index where the k is being applied. Remebering that b is not a true index, we do array[(b-1) + 1]. The b - 1 gives us the true index of the end of the subarray that k is being applied to. But simplifying it gives us b as the index we need to use.
First Loop, Pt 2 and Second Loop
This part of the loop, in my opinion, is the hardest to understand. The start of the subarray is array[a - 1], which was explained up top. This marks where, in the second loop, sum will add that value to itself. Ex. 0,0,100,0,0,-100,0. This is an example of 1 query array implented in the way shown in the above solutuon. Think of of sum as a way to temporarily view what the index would look like at that period in time and adding numbers to starts this. For example, when the second loop hits the index with 100, sum, until it hits the -100, will be 100. Imagine the zeroes are basically saying "No other modifiers here like another set being in this set or an end of another set in the middle of this set". The -k at b is very interesting. The solution above says its a marker but it serves to strip the sum in the second loop of k. This is saying that because the loop has ended, we need a way to make the next indexes reflect that it has ended and remove k from the ongoing sum. The combination of multiple implmentations of this will allow the array to take full shape. We then find the max based on these multiple implmentations. It can be like the index viewed could be 500 but if you were to break it apart it could actually be (300 + -100 + 300). Which could mean many diffrent things, the 300s could repersent the start of diffrent sub lists and the -100 repsersents the end of another list. They all come together to repersent a mean. Adding this to a sum allows to find a max which will give us our answer.
Conclusion
So, in conclusion, the solution handles the array like diffrent points in time and adding up these points allows us to find a max.
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 →
Disclaimer: Please correct me if i am wrong, I am just trying to provide a different explanation in the way that it clicked into my head while viewing your solution.
First Loop, Pt 1
First Loop, Pt 2 and Second Loop
Conclusion
So, in conclusion, the solution handles the array like diffrent points in time and adding up these points allows us to find a max.