 What's up guys, mine is Michael in and welcome to my YouTube channel today. We're gonna go over this problem called functions again it's uh code forces round 407 functions again and Pretty much. This is you we don't even have to read this first BS story, but pretty much. There's this function called I don't know what it's called. What is it some? Mathematical function F okay, let's just call it f. Okay, there's a function f that takes two numbers l and r And what does it do? It sums up all the numbers from l up to r minus one and it takes the each Position each at each index of I subtracts it by then the one right next to it and then multiplies Multiplies It takes absolute value of that and then multiplies by negative one to i minus l. Okay, so Pretty much our job is to calculate the maximum value of f among all possible values of L and r for the given array a So what did I do first to try to solve this problem? Well, I tried to first brute-forcing So what does that mean? That's literally means try every single possible number, right from one to n Pretty much in my array. So I loop from one to n then I calculate it. I passed in each each value of So I loop from one to n for for l passing one to n right and then I loop from r Which I set equal to l plus one to the n as well And then I pretty much just had a third for loop that calculated every single Every this this equation of f every single value of f by passing in l and r in this equation Over and over again, and I got tle I think I got so I actually got so I got tle on the Third case right here. I think wait. Let me see Well, I actually submitted so many times here the way more than what's showing here, but I think I got tle over So I got tle over the third test case the ninth the ninth test case. Yeah, ninth test case. I got tle so How do you do this problem? So what did I try first first? I tried doing like a I tried doing a I Tried writing out all the possible numbers of each of these problems of Every single summation and then try finding a pattern and I found out this pattern that if you were to use There's something called dynamic programming right diamond and programming I found this if this recurrence that occurred that if I were to store one number I would able to I was able to get to to pretty much to instead of Recalculating this equation over and over again I stored one of the last sums and then I was able to find something of that Right instead of instead of calculating over and over again I just stored the previous value and I did that and I also got tle because there was two four loops there So I actually it took me a while, but pretty much I had to look at it the editorial. Okay, so I'm gonna explain the editorial solution They don't provide a code for this problem. So it's actually nice that you can't just copy and paste the code but Pretty much. I'll explain the editorial solution here in pen and paper So I'm gonna explain it with you guys now. Alright guys, so I redrew the not redrew I basically drew every single possible combination of passing in one Lnr. So passing in one two then one three then one four one five into Lnr And then two three two four two five into Lnr and then three four and three five So here's the whole summation that I drew Currently, okay, so you see here from f of one two you get this this Equation a of one minus a two absolute value times negative one to the zero power Right, and if you see it from f of one three you see the exact same equation here And the only difference is that you're adding this this value plus a two minus a three absolute value times negative one to the one and After seeing this if you see F of one four, it's exactly the same as this here so this It's exactly the same as f of one three Right as you can see here. These two are exactly the same except they add another term in the end as well for f of one four They add a to the three minus a to the four times absolute value times negative one squared and F of one five is exactly the same thing except they add this last term So what they did was they took f of one four and put in here and they add a of four minus a of five times Negative one to the third. Okay, so you might be thinking okay, so I could probably store some number of values So don't have to re-compute these These equations every every single time right I could just store one of these Okay, but now before we had jumped to conclusions. Let's let's go to f of two three. Okay, so wait so an f of two three Right, it's a of two minus a of three absolute value times negative one to the zero Okay, so if we see from here This equation exact is exactly the same as f of one The last term of f of one three Right, the only difference is that this there's an exponent here There's a difference of exponent here for f of one three the last term was negative one to the one and in F of two three this term is the same thing except it's negative one to the zero So I know that whenever I change the left My left term I'm gonna be changing the exponent Switching the exponent over and over again and if you see and then the next in the next f of two four They put this this is a this is a they put it this term is exactly the same here Except they add the only difference is they add this last term and this last term We already know this this last term. We saw an f of one four over here and three minus a of four times negative one and the only difference is this power is two and In f of two four this power is one, right? But they still computed absolute value of a of three minus a of four absolute value so pretty much if we If we think about this the only thing differences between the numbers is that is The the exponent, right? The terms themselves this term is exactly the same as this term So these terms themselves are still the same. So the only difference is this exponent As we could see here these these exponents are the only difference in these terms. So I can technically What I could do is I could create two arrays that store these Individual terms every single term of these right where I subtract a one minus a two the next term from the first term right a be an absolute value and I pretty much do that all the all these terms For the every single other term and then I only change is the exponent, right? So I'm going to alternate positive negative positive negative positive negative for all the even values Right because we see here from when a f of one three right it goes It goes positive then negative right and that's for so the first term is positive and the second term is negative for f of one three So that means that I'm going to start positive than negative so For one three and for the for the values of one two one three one four one five I'm going to have an array that stores positive and negative and then if you see here It's also still positive and negative But let me let me see through two minus two zero. Yeah. Yeah, so but in this one it's changes because this term now is For effort to three right a of two minus eight of three absolute value times negative one to the zero This term is the same thing as this term right of effort one three except this is now positive And this is now negative right so I could alternate the values because I have one array where it's positive negative positive negative positive negative for Every single terms where I calculate all the terms of these then I have the exact same array in the next array And I calculate the same terms The exact same terms, but I'm going to alternate from negative to positive negative positive negative positive and what does that tell me? Well if I were to get the maximum sum of The positive negative positive negative positive negative, right? And then I find that maximum sum And all I could do is just add that with the alternating of negative positive negative positive negative positive, right? if you think about it because if there's only So most of these just like alternative positive negative positive negative and because there's only two ways of this could be doing, right? When you see from if I'm looping from one to five, right? In the terms are positive negative positive negative positive negative and then from two to two to the five I didn't put two five. I didn't go up to five But if you go look at two to five, it's like negative positive negative positive negative positive so if I have two arrays and then I just have Alternate the signs then I find the maximum sum of the the positive negative positive negative and Then the second array I find the maximum sum of the negative positive negative positive then I find those two values I could just add those two values up and then That will be my answer. So that's basically how to do this So I'm gonna show you guys how an example of how you would do this With the same Array that was given to us. Okay, so I'm gonna get another sheet of paper. All right guys. So this is Basically, I wrote an equation down from that is exactly what it will be just said before so When this is the index where I index from one to five and this is the array for one four two three one so I'm gonna have maintained one array, which is when the One L is odd, which is remember L's are starting from them iterating from Right, so remember based on our previous example here, right? When our L was odd, it was positive negative positive negative, right? When L was odd, it was positive negative positive negative, right? If we started at zero and then this was yeah, this was that that was the X. So I'm gonna start when L is odd I'm gonna do this equation why I'm gonna store an array that has I'm gonna subtract the right from the left and So I'm gonna do subtract four minus one and then two minus four and then three minus two and then one minus three, right? And I'm gonna alternate the signs. I'm gonna alternate when This is gonna equation when negative one is to the ith power, right? So I'm gonna start with positive negative positive negative positive negative or I think this is Yeah, okay, and then when for C Is my second array is when L is even and I'm gonna store the exact same thing except I'm gonna do I plus one So it's a different sign So it's gonna be alternating signs for these two arrays. Okay, so I'm gonna store the array right now. This is B and this is gonna be C So what is the B of I B of I so B of at one is going to be Four I plus one which is four a of four a of so I plus one is gonna be four minus one Absolute value is three three times negative one to the first power Which is going to be three times negative one to the first power is going to be negative three Okay, I Think I think so. How does he want us to do? I think that's right. Yeah, I think that's right. Yeah, that's right Okay, then the second one. I'm gonna do the same thing two minus four What's two minus four? This is gonna be negative two absolute values positive two then it's gonna be half positive Exponent because two negative one to the second is positive. So that's gonna be two Three as so for B of three, I'm gonna take the next term minus first one. So three minus two that's gonna be negative one Actually positive one wait three minus two. Yeah positive one absolute values one then To an odd power is gonna be negative. So that yeah four, let's look at four Five minus a one minus three is gonna be negative two absolute value is gonna be two two times negative one to the fourth is going to be Negative two I think wait, let me see two times negative one for you negative two Yeah to the fourth power, which is positive. Yeah, so one minus three positive value two Absolute values two two times negative four negative one to the fourth power, which is Positive yeah should it should be that I think this is I think that should be positive It's one four. Okay Let's got C now Same thing only differences alternating Yeah, so Yeah, yeah, so yeah same thing so now we just have to find the maximum sum maximum some segment of Each individual arrays and you could do this with either two types of algorithms You could use sliding window or you could use prefix sum Which is a dynamic program at prefix sum. So I'm gonna generate a prefix sum based on these individual arrays. So here I'll use a prefix sum Prefix sum of five. Yeah prefix. I'm a five. Okay, so prefix some I'll show you guys maybe I'll go in another video and pretty much explain how that works, but I'm basically just gonna add each Each term with the the right hand term. Okay, so and I'm gonna store it at the Index of the right side. So I'm gonna start negative three three A first term is the same and then I'm gonna add the second term to it So it's gonna be negative one and this is going to be positive one and then from negative one. I'm gonna add the Third term with it, which is negative two and it's gonna be one plus two positive two Now I'm gonna add the fourth term with it, which is gonna be negative two plus Thank you plus two which is zero and this is gonna be Two minus two is gonna be zero. Yeah, okay, so that's a prefix sum And now I just need to find the maximum value of both of them in this case It's zero and then three so it's gonna be zero plus three Which is you three and that's the same answer as what we got what the output was So yeah, now all we just have to do is code it up and I'm gonna show you guys how to do it All right guys, so I'm not going to recode it up again So this is my solution of what I did I'm just gonna explain line by line And I don't want to type up everything again over and over again, but here So I'm gonna explain line by line. So I'm gonna read in the my Size of n which is my size of my array that I'm gonna read in I'm gonna create a vector of the array and it's gonna be n plus one And the reason why I'm gonna do n plus one is because I'm gonna index from one one to n, right? So I don't want to do zero indexing because zero indexing is kind of pain in the butt especially with a problem like this like if if if the given input statements are One base index one base. That's why I'm gonna index everything by one Okay, so I'm gonna loop through from one to less than or equal to n right and I'm gonna read in every single Every single element in my vector array that I passed in that to read in from I'm starting with index one and then up to n right and we're able to do this because we start with their data at n plus one Because we are indexing from n plus one But anyway, that's what I did here and then what I did was I'm gonna create two arrays B and C B was the positive when B is with the is when L is odd and C is when L is even right and Then I create another two arrays, which is the prefix of B and then the prefix of C. Okay, and they're all indexed n plus one So so I'm indexing from index one. Okay, so in this for loop. I'm gonna loop from one to n and I'm just gonna calculate every single value of Each value beside it, right? I'm gonna subtract the the value So I'm gonna subtract the value from the right side from the left side minus the right value the right side, right? So okay my bad. I'm gonna take the value from my right next to my right right right next to my Index I'm currently at the position right next to me. I'm gonna subtract by that by my current position so what I'm gonna do here is I'm gonna Take a data at I plus one and then subtract by by my current position I'm at right and I'm gonna take the absolute value then I'm gonna multiply by power of negative one to I and I is the The what we're indexing from we're starting from one to n, right? And that's basically the the value that we store every single time of a of I plus one minus a of I right And I do the same thing for C except the difference is that I'm gonna do power of I plus one I plus one and The reason why I'm doing I plus one here is to alternate the powers, right? So the if the top part was positive negative positive negative I'm gonna alternate from negative positive negative positive, okay? And if the top one was negative positive negative positive, I'll alternate from positive negative positive negative, okay? That's what I'm doing here, okay? Now I'm gonna need to create my prefix sum array. So I start with my prefix sum array to have the value of one my bad the first value is going to be the same value as the Beath value right the first value of the B array and the second My set of my second array prefix C first value is gonna have the same value of the First value of the C array, okay, then I'm going to create my prefix sum and Yes, these two these two are just to find the maximum value Maximum value of the array of my prefix sum. So I'd be max versus the maximum value that I'm gonna calculate I start at zero in the beginning just initialize it and see max I'm gonna start at equal zero and that's going to be The maximum value of my C prefix C array, okay? Then I'm gonna create my prefix sum array. So I do for I equal to I less than equal to n I plus plus and I'm going to add Every single value with the value right next to it now with the previous value with the previous value my bet, okay? And that's going to generate my prefix sum array. So then now I'm gonna have every single like some sums that was calculated previously then I'm gonna take my max element of each one The max and C max and after that I'm gonna add both of them together So what I got my max element of my prefix sum array and my max element of my the C max of the prefix C array Right here these two and now once I have both of them I'm gonna see out and add them together and I already got mine a seed. So yeah, that's My this solution already got accepted. So yeah, that's basically how you do this problem rate com subscribe. I'll check you guys later