 What's up guys? My name is Michael and welcome to my YouTube channel today. We're going to do a problem called aggressive cows So basically you are given a barn n and the stalls are located along a straight line of x1 to x of n The cows don't like each other and they become aggressive to each other And we want to assign the cows to the stalls such that the minimum distance between any of them is as large as possible So what is the minimum distance? Okay, so to do this problem You first have to understand what the problem means. So let's actually just draw out the barns. So in this case we had We had five stalls and then we want to assign three cows and Let's label each of the stalls. So here we have one two One two four eight and nine. Okay one two four eight and nine So now we have five of these stalls, right and basically we want to place three cows such that the the distance between any of them that we place is as Larges as possible and we want to find the smallest of those. Okay So In this case You would put one cow over here at one you put one cow here at four and you put one cow here at eight and If you were to subtract the distance between any of these this distance is actually three and this distance of four and eight is four Right and the minimum distance of in this arrangement is three. So the answer here prints out three So basically we want to assign the cows in each of these stalls such that we want the longest Any two stalls has to be as large as possible, right? So between one and whatever stall you sign you want to make them as Longest as possible right from here just to here or here to here which one it is, right? This distance has to be as long as possible for each of them and then after that we are going to Find the minimum run one right? So we're going to print out the minimum one So in this case the minimum one between all these is three. Okay So in this case, that's what they did Okay So how do you do this problem? This problem requires something called binary search So what we're going to do is we are going to look at our test cases. So in our test case the largest range is actually One trillion I think so one trillion. So what we're going to do is we're going to have a number between one and one trillion Okay Just I don't I'm not counting how many zeros are okay, and then we're we are going to guess into a midpoint So we're going to guess into the middle of this. So this is going to be our guess. Okay. This is going to be the middle Okay, so we're gonna go to the middle and guess this number this number is probably five hundred Thousand let's just assume it's five hundred thousand for the sake of it. Okay, so we're going to guess five hundred thousand What we're going to do is we're going to go through all of our All of the numbers of our stalls Right, and we're going to check does this work right if it if it doesn't work We're going to move our right range down To the middle again. So in this case our range originally it was one to one trillion So if it doesn't work, we are going to move our range down to here and then guess the middle again So we're going to have a middle here, and we would guess here if it doesn't work Okay, it does work. We are going to move our bottom range up. So if it does work, we're going to move this start range up and then guess that okay So this is going to continue until our start and our end is Start is greater than or equal to end. So this is where as our start. This was our end So, yeah, that's what we're going to do. So how do we check if the number that we're guessing works or not? Okay, so what we're going to do is that remember we want to assign three cows Here or in whatever case it is see cows, right? So basically we're going to keep track of the count of our cows So we have to account we have to count our cows, right the number the count of our cows And we have to keep track of this. So if we're going to place a number Each time we have a cow, we're going to place it there, right and see it does this work So if if our guess is between Is actually the right guess, right? So let's say this guess is between the right guess We are going to increment our count because that means that we place a cow there, right? That means we place the cow there and then In the end of the iteration if our count is equal to See which is the number of cows that we have then that's good Okay, we're going to set our new answer to that to that number and then continue right if it's not Then we have to keep doing our guessing. So we have to keep guessing here and here and here, okay? So how do we keep track of the count? To count the number of cows replacing each time so here in this case What we're going to do is we are going to subtract the difference difference between Each of our the the our stall number. So here we have one two four eight nine So we're going to subtract the difference between two and one So this difference is by one, right? And then now what we're going to do is we're going to check our guess Does is the difference that we currently subtract is this less than or equal to the guess that we did Right because if it is less than or equal to it Then we're going to add one to the count and then we move on to the next one then we move on to the next one Uh, move on to the next one and then subtract the difference between this one Right and then we move on to the next one. Oh yada yada yada Okay, so like if this and the reason why this works is because if our guess is less than Or equal to the difference. So like uh, this is the uh, that means if we guessed it correctly, right? If our guess is less than or equal to the difference Uh, if our difference is less than or equal to uh Yeah, if if our guess is less than or equal to difference then that means we guess correctly because if it's too large So like 500,000 this is too large in this difference of one, right? So then then we have to move the range down, right? So yeah, that that's what we do. That's pretty much just of this. Um, until we get to the end Of our iteration, right until we get to the end of this, um Yeah, then then that's when you keep going. Okay, uh, by the end of that Our start and our start and end is going to be placed at the right one and then we will have the right count We would have the right number to for our answer. Okay, so that's basically basically just of this problem So i'm just going to explain the solution to this problem now so here We read in the test cases t then we read in our number of stalls and our number of cows. Okay So that's what this is doing We are going to read in data, which is the stall number, right for each stall number So now we're going to read in the number of stalls stall number We're going to sort the stall number because the reason for that is because let's say the stall number is not the right order Then yeah, that's not going to help you. You want the stalls to be sorted okay All right, so now we have a start which is beginning the range that we're going to guess and the end is going to be The final final range that we're looking for. Okay, the final range that is the final range the final possible range Okay, now we have our answer and this is the answer is going to be the one that actually works This is a variable answer. So while our start is less than or equal to end This is for binary search to work. Um, we're going at the midpoint So we're going to add the end and start and divide by two and that's going to be our guess Remember mid is going to be our guess. We're going to check. Does this work or not? Okay So now what we're going to do is that we're going to keep track of the number that works right the number that uh How many times does this actually work for the stalls? So how for each of these stalls we have to keep track how many of these actually work So in the end our account if it's equal if it's uh, Actually equal to c then that'll be the number of cows that we do So this is actually how many cows you place that actually works, right? Okay Previous is going to represent Uh the previous number that we are subtracting. So here in this case We have one and two right uh previous would point to here and this is going to be our current subtraction So if this works, then we're going to move our previous to this part and then our Subtraction our next number is going to be here, right and then we subtract. So if if previous works This previous works, right? Uh, so if this guess actually worked Then we're going to move to the next number to subtract And then the current number here. We're going to subtract it and see if this works. So that's what this previous is supposed for Okay, so now we're going to loop from one to n So we're looping now from one to n which is going to represent the Stalls that we're looping through so all the each of these stalls. So we'll be looping through that Now we are going to check is our guess remember mid is our guess, right? Is a lesson or equal to the difference between the current number and the previous number, right? Because if it is equal to just uh the difference, then we're going to increase the number that works, right? So that's the number times that actually works For it because we want we wanted to work for all these, right? We wanted to work for c of these cows, right? So that's why we do that Okay, so that's going to be our like our count. So there's a number of works How many of these actually work so yeah increase that Then we are going to move our previous to equal to the current value. So that's going to move the previous Uh, yeah, so that's going to move so originally, uh Previous was here, right? So if let's say this 500,000 actually did work Which it doesn't work, right? Because it's too large than the difference. Let's say it is it does work Right if the difference is right And what we do is we're going to move our previous to here And then we're going to move our the next number that we're going to subtract for from two to go on to the next one So that's what this is doing. Okay Okay, so that keeps counts how many This loop counts how many that works or not? Okay, so by the end If the number that works is less than c the cows are replacing um Then we that means we didn't guess right so we're good that that moves That's what that's going to mean is that this uh, our guess number 500,000 is too large. So we're going to move our end down Right our end is now going to be not be going to be this end We're going to move our end to equal our middle middle. So that's going to move here. So that moves the range down To start an end. So that's what that does. So then now you pick the middle point of this So we're going to keep picking the middle point over and over So that's what this is doing. Okay. So now otherwise Um, so if our number works actually does work So let's say it equals to c or it's greater than equal to c which is not possible But let's say it is greater uh greater than equal to c right That means we could place that number of cows Into the stalls, right? That's a number that actually works Then we're going to set our answer to equal to the middle, which is the guess that we did right The area answer is going to equal to middle and then we're going to move our start up So that's going to move the start This start is going to move up. So our start At that point the start So let's say it works. So our start was here And this was our end and this was like one and then I don't know 100,000 right Um, so let's say the let's say let's say it actually worked. So let's say let's say This 500,000 worked Well, if 500,000 work, we also want to we want to get the We want to get to the best possible value that does work, right? So this could work, but we want the best one. So that's where we Set our answer to the other guests that we did and then we're going to move our start upwards So this start is actually going to move upwards to 500,000 And then we'll see Can we do it? First from start to end of Here, right to for 100,000 and here. Yeah See if it works. Uh, these numbers I didn't add enough zeros, but I don't know. I don't I don't know the difference between that But yeah, then um Then we move our start up and then our answer Reason for this is because we want the largest minimum distance, right? So that's why we do that. Okay Um, yeah, move the start up and then at the end of the search sooner or later This search is not going is uh at the end of our binary search is going to end and then we just print out answer So that's basically how you do this question. Hope you guys understand the problem. Ray com subscribe I'll check you guys later. Peace