 What's up guys my name is Michael and welcome to my YouTube channel and today we're gonna go over prefix sum array So basically I had to use this prefix sum to solve another problem And I didn't really get to explain it to you guys how it works, but this is a pretty easy It's kind of like an introduction to dynamic programming more likely what we're gonna do is let's say I have an array We're actually gonna index it by zero. Okay, so zero one two three four and let's have the numbers zero one two three four five For simplicity sake, and I want you to sum up all the numbers given an index Okay, so let's say I have index is equal to three. I want you to sum up all the values from zero to three Let's call this array a okay. We're gonna call this array a okay So like if you're gonna think about it how to do this, okay So how would you do this? You would probably sum up all the values from one two and then up to three You know, okay, okay, that makes sense make sense, but here's a problem is that this takes a lot of time Let's say I'm giving you index five I have to literally add up all the values from zero to five right and to do that You would have to use a for loop right and then you would have to literally add up every single value up to zero and five so Look, so if I add up if I give you index five and then I have to add up all the values from zero to five zero one Two three four three four and then five But here's the thing is that whenever you give in an index you're actually repeating values that you're already computed Like it when I gave you index five you already Computed a zero plus a one plus a two plus a three you already did that in index three, right? so Recomputing this takes you what's the point of recomputing the exact same thing over and over again when you don't have to Right like let's say I gave you index after I gave you index five Let's say I give you now I wanted to find the value sum of all the values to up to index four Well, you would have to sum up this again and again and again plus a three plus a four But you already computed this before in the previous index that index five from zero one two three four All the differences that you did was add an a five So that's what a prefix sum fixes pretty much what a prefix sum does it For every index it stores the previous sum added to the current index So let's say it in this this array a right I want to create a prefix sum prefix sum of a so what do I do? I'm gonna store every single index So let's say index zero one two three four five. What am I gonna do? I'm going to store every single index with the sum of its previous value of the previous sum Okay, so based on what we see here, right when we're back up here index at index three and index five index four You see that these previous values. You already computed it a zero plus a one plus a two plus a three, right? You already computed this if I want to find index four. I Could just plug in whatever my sum was at index three Into here. So like my sum at index three Plus a four then I don't have to recompute this Over and over again given another index and then recompute it again and again and again, right? So here we're what we're gonna do for prefix some we're gonna store every single sum with its previous Sum that it add up. So so in the beginning, we're gonna start with zero zero Reason wise because the first value of our a zero zero and our previous sum is zero, right? Like there's no there's nothing before it. So we're gonna have zero now. What am I gonna do at one? I'm gonna add up my previous sum zero plus one. So it's gonna be have value one So my previous sum remember to zero and I'm gonna add it with value one now What am I gonna do? I'm gonna take my current previous sum one and add it with the new value that I have now I'm getting at index two. So I'm just gonna have three, right? One plus one plus two is equal to three, right? So now I'm gonna store that at index two and then now I want to up find the sum up to three So what I do I take the previous sum that I currently computed which is three add it with my new value that I got At my array. So three plus three is six. So that's six and then I'm gonna do the same thing over here So we have prefix some sit my previous sum is six I'm gonna add up the new value that I got in my new array for four So that's gonna six plus four is ten and then I'm gonna add another value of my prefix some ten Right. My previous sum is ten plus a at five my new value at five. It's gonna get you fifteen So I just Pre-computed all these values of zero one two three four zero one two three four five and all these values will actually have new Sum if I given an index so let's say I give you the index three Well, then all I have to do is just return this value which is six, okay? And if we look back at it, let's go back to our sum of our array originally Does this actually equal to the sums of all the values up to the index? Well, let's see Zero, let's sum up all the values from at index zero to three So let's do zero. What's the in that? What's the value at index zero? It's zero plus one plus two plus three and this is equal to six so that I Prefix some does work. It actually does compute all the previous sums Added up together up to the new index. Okay, so what prefix thumbs some does is it allows us to pretty much You got a previous value stored every single time when we have a new some so like let's say in this case Index three and index four. I'm gonna after I calculate this sum I'm gonna store it and then into a summary so then I have the sum three and then I add it with a new value So that's what a prefix sum does. I just created a prefix some array of this Yeah, let's go to the code of how you would implement this. Okay. All right guys So rather than just rewriting the whole code and creating like a new class and stuff like that I'm just gonna show you the implementation of geeks for geeks. So how does this work? so what we're gonna do is we're gonna pass in our Array into this function, which is our the original array that has all the values in it and figures the size of the array The end is the size of the array. That's what they did here And they also pass in a empty prefix sum array. Okay, so how do you how do you how do you start this? Well, you first just like but we showed you before we first set the prefix sum at the zeroth value to have the exact same Value at the value at the array at zero. All right, because in the beginning your prefix sum is there's What is it at the previous value? Well, the previous sum is zero So zero plus our current value at our current array is going to be the exact same value as our current array So that's why we have the prefix sum at zero is just going to be the same value as our at the zeroth value of our current array Okay, so that's what this does this first statement does Then what are we going to do? We're going to loop from one to n and the reason why we loop from one end is that we're from one to n What we're going to do is we're going to take the previous value the previous prefix sum that we Previously calculated the previous sum that we previously calculated We're going to take the current index i minus one that we previous calculated to get the previous sum at the previous index Right, and then we're going to add with our current value That we have we're going through in the array and then we're going to store that into our new prefix sum at our new value of i Yeah, so this is going to go from one to n and it's going to fill up all the values of the prefix sum and Yeah, so to do this you could just call this function pass in these two elements of previous sum you and Yeah, you pass in the elements the prefix sum it will create your prefix sum array for you and then Given an index you probably could just return whatever Prefix sum at that specific index and that will literally just return all the values of The sums all the values from zero up to that index. So yeah, that's how you do this That's basically how this code works. It's I hope this video is pretty helpful for you guys It's pretty quick video, but yeah rate comment subscribe. I'll make another video today later today, but yeah, peace