 What's up, guys? Mine is Michael, and welcome to my YouTube channel. Today, we are going to go over array manipulation. Basically, you start with an array of one index of zeros and enlist the operations that you have to add to each element between two indices. So once the operations have been performed, return the maximum element. So basically, you're given the array of a length n, which is here we have 10 zeros. And then these are the queries. So we have 1, 5, 3, 4, 8, 7, 6, 9, 1. So as you can see here, the first query, the first element of each of these, is going to be a. And the second one is going to be b. And a and b represents the indexes that we have to add. The indexes that we're going through, and we're going to add the element k to it, the third element k to it. So here, as you can see here, we're adding 3. In the first case, we have 1, 5, and 3. So when our array starts at all zeros, we're going to loop through from 1 to 5. And we're going to add 3 to all of them. So that's where all the values here are all 3. Then from 4 to 8, we're going to loop through from 4 to 8. And we're going to add 7 to all the values. So here, from 4 to 8 here, they added 7 to all of them. So that's why we have 3 plus 7, which is going to give us 10. 3 plus 7 gives us 10. 0 plus 7 gives us 7. 0 plus 7 gives us 7. 0 plus 7 gives us 7. So now these are all elements we have, right? Now we're going to have 6, 9, and 1. So this is going to loop through from 6 to 9. And we're going to add 1 to all of them. We're going to add 1. We're going to add 1 to 7, 1 to 7, 1 to 7, 1 to 0. So now in the end, we have 3, 3, 3, 10, 10, 8, 8, 1, and 0. And then what they're asking is, what is the maximum element after all your operations are done? So in this case, 10 is the maximum number out all these in the end, so they return 10. All right, so now what is our element supposed to be? What are we supposed to do? OK, so how do you solve this problem? Well, you could implement it like how I just stated, but you're going to get TLE, too long, time-determined. So here's what you can do, actually. So let's say we have all 0s right here currently. And let's just go through each of the queries. So here, let's say we have all 0s. And let's say we're just going to go through each of the queries. So what you can do, the issue why it gets too long determined is because there is a second loop that actually causes it to have too long. So here's the reason why it gets terminated because it's too long. So if you look at here, if I look at here, and I just try to implement it by itself, I create an array. I loop through all my queries. So I'm going to get my A, B, and K, which is at the 0th index, 1 second index. Then I loop through from A to B, and add by K. This is the loop that takes the most amount of time, adding by K between the two indices. So what we can do is we can actually try to get rid of this loop. So how would you get rid of this loop? One thing that really matters, remember, what we're trying to do is we're trying to get the maximum number after we add K to each of the indices between A and B. So what we can do is we could actually just ignore all the values in between and only focus on the endpoints. The endpoints between A and B are the only ones that matter. So what I'm going to do is I'm going to actually just only care about the endpoints and see what happens. So here, let's look at this example. We have 1, 5, and 3. So I'm going to go through my array of index of 1, and I'm going to just add 3 to it. So instead of adding all the values between 1 and 5 of indexes of 1 to 5, I'm just going to add only care about the endpoints of 1 and the endpoint of 5. So here, the first endpoint is 1, so I'm going to add 3 to the first value. Add 1, 2, 3, 4, 5. Now the second element is 5, so I'm going to add 3 to the fifth element. So yeah, 1, 2, 3, 4, 5. I'm going to add the fifth element, 3 to the fifth element. OK, so now that's 1, 5, and 3. And now we have 4, 8, and 7. So we're going to add 7 to the fourth element and 7 to the eighth element. So we have 1, 2, 3, 4, fourth element. We're going to add 7. And then 7 to the eighth element, so it's 1, 2, 3, 4, 5, 6, 7, 8. Make this a 7. And we have 6, 9, and 1. So we're going to go through 1, 2, 3, 4, 5, 6. Oh wait, let's say 1, 2, 3, 4, 5, 6. Add a 6 here. Actually not 6. Add a 1 here. And then 9, we're going to go to the ninth element. 1, 2, 3, 4, 5, 6, 7, 8, 9. Add a 1 here. OK, so by the way, careful that in arrays we have a general index to add 0, but in this problem it's indexed by 1. So be careful about your arrays. Anyway, OK, so now that we have all these values here, since we're only caring about the maximum sum, not maximum sum, the maximum number after all these operations are formed, what exactly are these operations doing? Well for starters, what the operations are doing is that it's adding k every time, each of the queries, whenever we're going through it, whatever between a and b. Inclusive. So it's basically just doing a cumulative sum regardless. It's going to do a cumulative sum at the end. So what we're basically trying to do is we're trying to eliminate this loop and just have it at the end. So if I go to my submissions, we're just going to try to get rid of this loop, this loop, adding all the elements of k between a and b. We're trying to get rid of this and we're just going to care only about the endpoints of a and b. Well now that I am only caring about the endpoints of a and b, how do I get the maximum number at this point? Well, the issue here is that trying to get the maximum element at this point is pretty hard because at this point where if we're trying to just accumulate the sums, we're actually counting twice, right? So as you can see here, we added three twice and then we added one twice and we added seven twice, right? So we added three twice, add one twice, add seven twice and that's not good, right? Because if I'm just going to add these up, we have three plus seven is 10, 10 plus three. 10 plus three is, what is 10 plus three? 13, 13 plus one, 14, 14 plus seven, 21, 21 plus one, 22. That's all the elements of 22 are actually, that's not the answer, right? So if we're just going to accumulate the sum up, that's actually not a good answer. So what you can do is you could actually just set the second value to be negative so then we get rid of the sum in our final sum. So what we're going to do is the second values of here, we're going to make these negative. Okay, so then when I add the cumulative, adding up all the numbers up, we have three plus seven is going to be 10, right? Then 10 plus, 10 minus three is going to give us a seven. So is that the largest sum? No, it's not. Seven plus one is going to give us eight and is eight the largest sum? And also if you go back to the problem statement, it is one of the overlapping elements in our ending array, right? So that's actually good that we added negative three. We made it minus three. Is eight the largest sum? No, it's not. So we're going to do eight minus one, actually eight minus seven, which gives us one and that's this last element here, right? This last element here, eight minus seven is one. Is this the largest sum? Nope, and then one minus one, it gets us zero. Yeah, the gist of this code is that we just do this same thing. We only keep track of the endpoints and the last, the second endpoint, what we're doing is we're making those negative. So then if we make those negative, we're basically allowing do the cumulative sum in the end is going to allow it to work because we're not over adding twice. And we do cumulative sum in the end. Okay, so yeah, I'll show you guys the code now. So here's the code. Okay, so here I created my array, created current, the current sum, I created the maximum sum, maximum current is the maximum sum. I'm going to loop through all my queries and I'm going to get my A, B and K. These are the at the certain location. These are zero one and two, right? Because it's the first, second and third. Zero one, two. Then what I'm going to do is I'm going to set A at the location A minus one, the first one to be plus equal to K. So that's the first endpoint. We're going to add it by K, add it by three. That's the endpoint every time we're going to add the first endpoint we're going to add by three. The reason why it's A minus one is because I'm indexing from zero because I don't want to index from one. Now you could just do this at A but then you would have to make your array and plus one and I don't want to do that. So yeah, we have this. Now we need to, I did this loop if B is less than N because sometimes you get out of bounds. But basically what it's doing is like, it's just setting this, the second endpoint to be negative than my negative K, right? Minus three instead of, it'll just be second, the second one to be negative. And the second endpoint is going to be making negative so negative seven. That's fine, it'll be negative one. So that's what this, if statement does. Then I'm going to have a current sum which starts from zero and I'm going to loop through from zero to the end of my cumulative sum array and I'm just going to add up all the sums and then I do a maximum. Each time I add them up, I'm going to have store them as a maximum variable and then I just return it. So yeah, that's the gist of this code. It got eight seed. I hope you guys enjoyed this video. That's all there is for arrays. Then we're going to move on to link lists tomorrow. So yeah, peace.