 What's up guys my name is Michael and welcome to my YouTube channel today. We are going to go over candy one, which is an ad hoc problem This is the last problem. I'm gonna upload it for today, and then I'm going to do some of my homework But yeah, I may not be able to upload Every day now and there might be some days I have to upload like more than once But yeah, it is what it is. We'll see what happens. But anyway in this problem. Basically, there's a bunch of candies and Children will get angry if they don't have the same amount of candies and What Jennifer is trying to do as a teacher is she's gonna open every single bag of candies And she's gonna try to rearrange the candies so that every person get the same even number of candies right so Yeah, so the input line is the number of candies followed by the number of candies in each packet and then the Yeah, the Yeah, the number of lines in each packet and then the pretty much After that there's just The last will last number is negative one. So yeah So basically what we're gonna output is the smallest number moves it takes to make them all the same So we're gonna move packets in each of the array So we're gonna move each packets in each of the array and we want to have the smallest number of moves Created for each block of packet. So one move consists of taking one candy for one packet and putting it to another If it's not possible, then we output negative one. Okay So yeah, this problem is actually not that difficult. So I'll just explain it So to do this problem Easiest way, let's say I have a 1 a 2 a 3 up to n A of n and I want them all to have the same number. So the same number same number The easiest way to do this is actually to Easiest way to do this actually to get the average Because if the average if you ask sum them all up To a of n right and then divide by the number of size, right? if we sum them all up and Divide by n that's going to be the most even Smallest number of ways you could probably do it because they're all even at this point and then how far they are from this The average is going to be the easiest one because like if you make it the highest value, that'll be that's too high Right, the lowest value would have to get added way too much And if you make it the lowest value the highest value would have to decrease way too much So the easiest way is actually to make all the all the values here Become close to the average as possible because that's that's how that's the closest way to do it, right? If the average is like the most center Center value, right? So then if you decrease all them or increase all them it'll get to It'll minimize the number moves it would take to get to all them being the same So what can we do with the average if the sum of all? of a of i Right over n if this is actually not even so if this is actually like a double This is a double then we know that the the array a 1 a 2 up to a of n This array is not possible to the average is a decimal Then that means that this array you you can't you can't actually make them all even numbers, right? It's going to end up having like one number is going to be way too large in the other one And the other one would be way too large in the small one, right? So that's that's what would happen if the average is double so to check if it's possible or not We just have to check if the average is a double or not Now how do we calculate the number of moves it would take to actually make all these even assuming it is possible So if it if the average is like fine, right for if the average for this This is average if the average of all these numbers is not a double So that means it's possible to make them all Make them all equal to it all we have to do is just Subtract the difference it would take from each of these numbers with the average So the average would be like so we want them all to become the average We just have to subtract the average from all each of these right and then we add them up Right because that would be the number of moves It would take to move each of these to become the same number, right if you were to subtract The average from a one that would it's going to be the number of moves It would take to a one to either increase it up to average or decrease it down to average, right? If it would then we add it with how many times it would take a two to get to average, right because average is going to be The the value that we want all the numbers to end up being right We want all the numbers to have the same number as the average So if we take a two and subtract by the average That's going to tell us how many moves it would take for a two to either get to the average So you two would get up either you increase or decrease to get up to the average and when that happens all these values when you sum them up up to n and add up add up all the difference between the average difference between a of i and the average It would tell you if you add them all up It'll tell you the total number of moves it would take and this total number moves of the smallest because the average is the The closest one for all of them to be right the closest value that all these values can possibly be and It would be the minimum number moves it would take to get them all to be the same number So yeah, um, yeah, I'm going to explain the code now Now to do it. Okay guys, so I'm gonna explain the code to you guys now Okay, so I read an N and they said to keep keep doing this until your value that you read is Equal to negative one So that's why I did while n is not equal to negative one and then in the end I keep reading an end because that's when the condition ends right in the end. You have to keep reading n If it does equal to negative one it would stop and then it would not read in again But that's a really weird input statement, but that is how it is so I have some equals zero and Then I have the array of vectors of data and Then I loop through from one to n and then I'm going to read in every value of my array And then I'm gonna add to some equal to that. Okay, so some is gonna add all the values of the array I'm gonna get the average. I'm gonna store into an integer because this is gonna check if it's a If it's divisible by it or not, right? I'm gonna check that later But yeah, I take some and I divide by n to get the average So now here is where the stuff is gonna happen if the average multiply by n So if I take the average and multiply by n and it's equal to the sum that means it is possible to divide them evenly Otherwise I print out negative one because remember we wanted to check if it is possible or not to make them all the same value right the same evenly distributed to all of them, so that's the average and If the average is a double then it's not possible, right It's just not possible and you could check that just by multiplying it by the original number of n that you just divided by And it should equal to the original sum Right if this was a double then this would not equal to each other So yeah, then it would be negative one if they're in that case, but anyway now here's the gist of it We have number of moves that we have to add That we're gonna print out So originally when I made this video I thought that you had to subtract for every single value but now I think about it you're actually don't need to because You don't have to subtract every value of data from the average because all we have want to do is Redistribute the ones that are greater than the average Right, so the ones that are values that are way greater than the average you want to distribute that like if I have Let's say I have like I don't know two four One two four Five right so six plus five. There's this is 12. Oh Wait, no wait two one two three plus four. It's a 12 divided by four is three, right? So the average here is three Let's say I have this if it is Greater than the average four and five are greater than the average of three So I want to redistribute for all the values that are greater than the average to the other lower two, right? I want to redistribute this number. So what I'm going to do is I'm going to take four minus three So I'm going to take the current number if it's greater than I take it and then subtract it from the average average So I take four minus three and that'll take give me one One candy to donate to the other two So that would then this would become three and then the one candy I could just donate to here So this would become two and then I could donate this this also So five minus three is going to give us two and I could redistribute this two to the here and here Right, so it's you would only subtract Redistribute candies if it's greater than the average if it's less than the average you don't redistribute candies So that's why in the code here if the data of the current Value that I'm iterating through is greater than the average then I just sum up the number moves It would take which is going to be the num current number and subtract it from the average And then after that after I sum them all up. I just print out number moves and later on this actually got a seed So yeah, that's the code. I hope you guys enjoy this video rate com subscribe I'm done uploading today. Yeah, I'll check you guys later. Peace