 What's up, guys? Mike the Coder here. Today we're going to go over this problem called max array sum. So given an array of integers, find the subset of non adjacent elements with the maximum sum. So basically, we just need to find the set of find a subset where the elements are not really adjacent, right? They're not adjacent. So I have to pick if I have this, this array of negative two, one, three, negative four, five, right? If I pick one as a subset, right, I cannot pick three, because that's right next to it. And I cannot pick negative one, because that's an X to it. Also, right, they want non adjacent elements, and they want us to find the subset that has the maximum sum. So and then we have to calculate the total sum of that subset and return that. So it is possible maximum sum of zero, the case when all the elements are negative. So in this case, you could see a different types of scenarios. If I pick negative two, three and five, then the total sum would be six. If I pick negative two and three, the total sum would be one. If I pick negative two negative four, the total sum would be negative six. If I pick negative two and five, the total sum would be three. If I pick one and negative four, the total sum would be negative three. If I pick one and five, the total sum would to be six. And if I pick three and five, the total sum would be eight. So the maximum subset sum is eight. So yeah, and also note that every individual element is a subset as well. So yeah, we cannot pick adjacent elements. So I cannot pick like negative two and one and I cannot pick one and three, right? So I cannot pick like elements that are right next to each other. So that's the thing. Okay, so how do you do this problem? What we're going to do is we're going to create use dynamic programming because this is part of dynamic programming. So first, we're going to build an array of dynamic programming. And what we're going to do is the first element, we're going to set to the first element of the array, right, because that's like the first element. And the second one element, dp of one is we're going to set as the max value of the first element and the second element, right, the zero element, the first one. The reason why we're doing this is because that's like the maximum maximum element that we could start it from also, right? Like if I start from, so in this case, if I start from negative two, right, I could start from negative two, or I could start from one, right? I could start from negative two or negative one, or one to pick whichever subset I want to start from. And negative two, I could start from this one also. But I want the I don't want to just pick like negative two and then not the maximum number between both these, because if I just pick negative two, and pick negative two, or just pick one that's not going to be correct, because we want the second value, we want the maximum value of the first and second value. Okay, now what we're going to do is we're going to build a table. So the first first way we could do this is, is how you do it. We're going to start from two, and we're going to go to the end. And there's three choices you could have. There's three choices. One is if I don't pick the current element. So if I don't pick the current element, right, it'll just be the same as the last value, right? I'll just be like, okay, if I don't pick this, so let's say I have negative one, and negative one, right? And then I have one. That's I don't want to pick one. Well, if I don't want to pick one, and I don't want to pick this element, well, it'll just equal to the previous out of the previous sum, right, because if I start from negative two, I want to sum up the values for my subset. Those equal to the previous value of negative two. So that's why we have dp of I minus one. Okay, now the second choice is what we're going to do is if we increase our value, so we're going to add the current element to our current sum. So in this case, let's say I had negative two, and then three, right, so I have negative two, and then let's say I want to add the value three to my set of negative two. So in this case, I'll just add the value three here. So then the total sum of our maximum sum would be negative two plus three. Correct, it would be negative two plus three, because we're going to include include the current element in it. So this minus two represents like I minus two is going to represent the previous value of including the current element, right, I minus two. So I minus one is excluding, we're not going to include the current element. So we're just going to use the previous value. Since we're setting that previous value that as I minus one, I minus two is going to represent, including the current value, the current value. So I'm going to include the value of R of I of the current element. So that's why I do dp of I minus two plus our buy. And the third state you could have is, if I want to start a completely new sum. So if I want to start a completely new sum, so let's say let's say I have like, negative two, three and five, it's not that's bad value. Here, let's say I had one and five, right, one and five, include one and five. But also, I realized that three, and five is actually higher, right, three and five is higher. So in this case, what we're going to do is that we're going to start a new sum with our new value of our current element. So if I had like, I don't know, let's say I have one and negative four as a sum of negative three, and I want to start from one to five, right? In this case, if I start a new sum, then I'm going to have to go from like one, one, three is higher than one, right? So then what I need to do is I need to build off of my previous value as my includes a new element that I'm starting from. So that's R of I, right, that's the current element I'm on. So I represents the current L current index that we're going through the array, and R of I represents the current element that we're on. So if I want to start a new sum, then I need to just start it from itself, right, I'm not going to add any previous value. And I'm not going to just do that. So because we want the maximum sum, then we have to take the max of all three of these states. And then that will build up our DP array. And then our DP array will just the last element would just be the one that we actually want of our size minus one. So yeah, that's pretty much just this problem. I hope you guys enjoyed this video. Ray com subscribe. I'll check you guys later. Peace