 What up you two Mike the coder here today? We are going to go over Apple division now I know I've said that I will go back to coin piles and Tower of Hanoi But I think this problem is easier than the other two problems And I think this problem is good for you to understand It's just easier than those two problems And I think it's better to go over the easy problems first because I'm too lazy and I rather go over the easy problem first And I'll come back to that other two. Okay, I'll come back to other two. Don't worry tomorrow. We will go over Coin piles and Tower of Hanoi But for now, well, let's go over Apple division So what is Apple division given n apples with known weights your task is to divide the apples into two groups So that the minimum difference that the difference between the weight of the two groups is minimum So we have n is equal n Which is the number of apples and then we have each of the n integers of the weight of each apple so p1 p2 up to pn So in this case we have five is an input and then we have three two seven four one These are the weights of the apples and our job is to minimize Divide the apples into two groups so that the difference between the weights of the two groups is minimal Okay, and then we just print one integer, which is the minimum difference between the weights of the two groups. Okay, so What does this mean so let's let's actually just draw in paint first so let's say I have three Wow, I am so bad at using this touch pad Using the mouse the mouse is a really bad really really bad. Okay, so we have three Three two. Okay, you know what screw this screw this screw this screw this. Okay, I screw this Let's let's use this just type it out. Okay, so we have three two and typing is not working. Okay, three two Three two seven four one. All right three two seven four one Okay, so let's say that we want to split into two groups So what is the task again? We need to divide the apples into two groups so that the difference between the weights of the two groups is minimum Okay, difference between the weights of the two groups. So um, so three two seven four one are the each individual weight of the apple and we need Divide each of these apple weights into two groups, right? So let's say I for the first group. I just pick three two and Yeah, I just pick three two. Okay, so the first group. I pick three two. Okay, so this is the first group This is the first group. Okay. Now the second group I pick seven Four one. Okay, and the reason why is seven four one is the rest of the group, right? So if I pick three two seven four one will be the rest of the group because I mean, that's the rest of the group, right? So what is the what is the weight of the first group of three two? Well three plus two is five So let's just put the weight of the first group as five Okay, and then what is the weight of the second group? Well, the weight of the second group is seven plus four plus one So seven plus four plus one Okay, so seven plus four plus one is 12 I believe it's 12. Yeah, that's 12. Okay So now what they want us to do is that now we have the we split let's assume we split into these two groups, right now We we need to get the difference of each group So what is the difference between twelve and five? Well, twelve minus five is Seven so we're gonna do the difference is gonna be seven. All right, twelve minus five is seven. So this difference is seven Okay So this is assuming we split three two into one group and then seven four one into another group. Okay Now think about this. Okay? Let's say I split three two into one group and seven four one is the other group. Okay Um, what how do you calculate the weights of these groups, right? It's simple. You just add them up you sum them up, but I don't want to Recalculate like summing up three plus two right is equal to five. I don't want to Recalculate all every single time right so I have three plus two is equal to five, right? Once I have this first group I don't want to recalculate the second group also because I would have seven plus four plus one would be 12 and I don't want to loop through from this point to the end. Okay, so how would you do this in this case? So if you don't want to recalculate something what you could do is just calculate the whole thing at once and then Since you could calculate the whole thing holds some at once You don't have to recalculate the second group if you have the first group, right? Because the sum of both these groups are gonna be The total sum is gonna be the same no matter what right if you just add them all then up, right? You split into one group The other group will just be the total sum subtracted by the sum of the first group, right? So like if you look at this Three plus let's look at the total sum the total sum is three plus two plus seven plus four plus one So what is that? That's five plus seven, which is 12 plus four, which is 12 plus four 16 plus one is 17, right? So our total sum our total sum This is our total three plus two plus seven plus four plus one. This is equal to 17, right? So since I have this total sum of 17, right? And let's say I split this first group of three plus two equal to five Right. I could just take 17 minus five to get 12, right? I don't have to recalculate again for the second group because since I'm Since I have the total sum of 17 I just have to subtract by the first group and I would have the Sum of the second group. Okay. So yeah, that's that's basically the reason why that you could do this, okay? So now using this logic What we could do if we want to find the minimum difference between the groups, right the minimum difference between the groups Remember our task is to divide the apples into two groups so that difference between the weights of the groups is minimum So we want the minimum difference between two groups. What we could do is that since our Our inputs are so small. I is between one to 20 We could brute force and just go through every single element and pick those group Pick those values in the first group Then subtract total sum this total minus that first group to get the second group and then we could take the difference between The first group and the second group, right absolute value of the difference between the first group and the second group and then that will give us our Smaller distance smallest distance and then we could keep a counter a counter talking about what is the not a Variable minimum difference to keep track of that smallest distance Smallest difference right to keep track that smallest difference, right? So if I take if I have the first Group is equal to five and the second group is 12 Let's just do 12 minus five is equal to give us seven, right? And I just go through every single possible group Of the first group and then I subtract total sum from the first group to get the second group And I just subtract both of the sums of these of the second group I'm going to get a minimum A difference, right? I'm going to get a difference and then if I have a Minimum variable that keep track of the difference every time I have a difference smaller than that variable minimum variable That I could update my difference and that will give me the smallest difference the smallest difference between the two groups, okay? So, um, if you want to code this up, I will show you guys the code now So essentially is we have just have to go through every single possible Group group of the first group take the total minus the the sum of the first group to give our second group And then after that, um, we just subtract them. So if I were to do this, I'll show you guys what Uh, in words what this means. So we have the total sum. So we calculate the total sum, right? So add up all the numbers I'll write in words add up all numbers To get total sum, right? Then after that, um, We just brute force brute force all groups Of of first group, right? Then what we're going to do is we're going to get the second group second group is going to equal to Total sum minus first group, right the total sum Minus the first group and then After that, uh, our our difference, we just have to calculate difference. So then our difference Calculate the difference between the two groups calculate difference between two groups And what is that? That's going to be first group minus second group, right? So essentially that is this whole algorithm in English. So I'll just show you guys This is this would be the whole algorithm in English. And, uh, let me just wipe this Away first. Oh my gosh. No, no, no, I lost it. Wait. Oh, no, I got it. Got it. Okay. I lost it again. Yeah, add up all the numbers to get total sum brute force all groups of first group The second group is equal to total sum minus first group Calculate the difference between the two groups of first minus second and then we maintain a variable for the smallest difference. Okay? So that's essentially the gist of the algorithm in English. And then, um, yeah, I'll just show you guys the code now So the code looks like this Okay, so first, um So first, um Here I read in the, uh, n which is the size of the Piles of the, uh, the weights of each of the total piles, right? The total variable of of the piles of the apples, right? Total number of apples, right? And then I created this p This vector long, long of p of n and this is just going to have a store every single weight of each of the apples, right? And I have a total sum and then I'm going to read in Every single apple, right? Every single weight of each apple and then I do total sum plus equal to each of the individual weights. Okay Okay, um l l means long long. Okay l l means long long. I defined it up here l l means long long defined l O's long long. Okay, because I don't want I want to avoid overflow. Okay Now we are going to brute force Okay, we're going to brute force. So this is how you brute force Um, you go from i equals zero up to two to the n. Okay, and then Um, here is going to the second loop is going to go from j equals zero to n And what you're going to do is you're going to shift one, uh, one to the left by j and end it by i Okay, and what this is doing is that I went I had a video of going over this of a brute forcing every possible combination of uh, of Yeah, brute forcing every possible combination Of picking up in an array and I think uh, you could using bit sets you could check that video out if you don't understand But this is essentially what i'm doing and then uh If this is true This means that is uh, that's the value that i'm picking. That's the value i'm picking in the array Right, that's the value i'm gonna pick in the first group. So s s equals zero is You as the variable representing the sum of the first group. Okay So s plus equal p of j Uh, p of j is going to mean, um Summing up all the values in the first group that i'm picking. Okay, so This four-loop first four-loop is just brute forcing every single possible way of the first group. Okay, then what i'm going to do is um Here I do the absolute difference Of uh, total minus s total minus s remember is is going to be the value of the second group Right the second group. So total If s is our first group, right if s is the sum of our first group Total minus s is going to be the sum of the second group And then because i want the difference between the second minus the first group, right I will do this total minus s minus s again because s is the first group And then this total minus s is the sum of the second group. So the second group minus the first group Is going to be our difference. Okay I take the absolute value because they want us to do that. Um, yeah They want us to take the absolute value of the difference between the first group and the second group And that's what this I do here. I call this variable current. I call this variable current. Okay, then What i'm doing is I have this answer variable of equal to Infinity which is long long max, right? Long long answer is going to equal to long long max is basically infinity And I just reset this variable uh answer is going to equal to the minimum of Answer and uh current which is our our difference our current difference Okay, so by resetting this variable of answer equal to minimum of answer and our current difference That's going to reset the What that's going to do is that's going to get the minimum difference For the group. Okay The minimum difference between the two groups. Okay, and then in the end after since this goes This basically brute forces all possible groups of the first group and um gets the sum of the second group and takes the difference for each of them and The answer it gets the minimum difference In the end we later just print out our answer. Okay So, yeah, that's basically the gist of this code. Um, I hope you guys enjoyed this video. Ray com subscribe My throat hurts. I'll show you guys later. Peace