 What's up guys, my name is Michael and welcome to my YouTube channel today. We are going to go over replacing elements Which is part of the educational round code forces round one or two I Haven't been going over code forces in a long time and the reason why I'm not doing that is because I don't know It's just it takes a while for me to do go over some of these problems And I like to explain them and also so I haven't really been going into like the rounds so much I haven't been really competing that much But I did compete the last time which is this is this content just a few days ago. So yeah, I Did solve a and B So, yeah, I'm gonna try to basically if I get into habit of being able to solve a and B for a lot of the problems Then I think I should be able to get to green You would do a B or C right a B and C then I should be able to get to green. Anyway, let's so let's do this so Basically give an array of a 1 to a n right so these are Vibes of the array and you get to choose three indices ij and k and then assign the sum of a a j is equal to a k Plus a j right a yeah ai is equal to a j plus a k So you basically could pick two different distinct indices Add up the two values of the sum and then replace it as a I right so like basically Pick two did two different indices add them up Add two of the numbers up and then you replace the third one with it, right? So that's basically what we're saying here So now we're asked can you make a I lower or equal to D using the operation any number of times? So can I make all the values of the indices? I think yeah, all the values of indices less than or equal to D using the values the operation any number of times, okay? So I basically just brute-forced this So you could technically just go through every single value Every single indices right and then add Two of the indices up of J and K up and then replace the I and you continue doing that until that you reach the end and only replace it if you could replace it and make D as small as possible right like make a I small less than or equal to D So if that if AI is less than or equal to D, then you replace it, right? So, yeah But you also have to check the initial condition when if all the values are already less than or equal to D Then just print yes. So yeah in that case just print yes So I'll just go over the code because I'll just explain what I did. So yeah, here's what I did It's not much actually but Here so I read in the test cases of T Then I read in N and M and then I read in the values of the array Here what I'm doing is I'm gonna read in all the values of the array and I have I have a Boolean It to make sure they're all less than or equal to M and If they're all all less than or equal to M that means we don't have to do any operation, right? So like let's say we had Let me actually draw this out. So let's say Um Yeah, hold up hold up. So let's say we had like two Three one right and then D is equal to four, right? So remember what we're trying to do what the question is asking is that can we pick two indices? So like so let's say this is I this is J and this is K Can I like pick an I equal to two pick pick a random industry I right and then add two distinct other distinct indices So here let J is equal to three and K is equal to one, right? So take take the values of them have them up. So three plus one is two three plus one is four Then replace it with replace I with the sum of both of these Right, that's basically what the question is asking like the sum up pick have make sure you pick two distinct indices Sum them up and then replace your I equal to that, right? So Because we don't really need if all these values are already less than four We don't yet even have to replace Yeah, yeah, yeah, right. We don't have to replace any of the numbers If all the values are already less than equal to four then it really doesn't matter like we just have to print it out So that's what I did here over here. Um, this Boolean is just going to represent if all the values are less than or equal to M So what I'm doing is I'm and equals which is basically just Making sure that all these values are true, right? All the values are less than or equal to M And if they are then all the all less is gonna is can remain as true right and if they Do remain as true then I just print out. Yes. That's what I do Okay Otherwise, I'm gonna replace the values, right? I have a function to call the replace then I check it again If they're all less and if they're all all if they are all less then I print yes, otherwise a print no So this is a way to check if they're all less, you know, I basically to make sure all the values for each AI I make sure it's less than or equal to M Right, and I have a Boolean that just sets that if it is if it is less than or equal to M It will remain true. I do and equals Okay So otherwise I replace value. So here I have a replace value and I just brute-forced it. So The value n is which is the the size of the array, which in this case is it will be like three Right because there's three numbers one two three, right? Yeah, in this case would be three then I'm gonna loop through my all my values And I check if if I if my current value is greater than M So M is actually the number D. I should have used D in this case Yeah, in in my case, I wrote M is I wrote M instead of D But uh, yeah, we're actually trying to check D. Yeah, but I used M a variable M But yeah, um, so if if I find a number that's greater than than the value of M or D in our case, right? If I find a number that's greater than this This four I find a number in my array that's greater than this four. What do I do? I loop from the beginning again Then I find a number that is another number that's less than M So that's what I do here. I have to make sure that this index is not the same number as my original one So here, that's why I have a four into J is equal to zero and I go through J is less than M right I loop through again and Then J plus plus so here this if statement is checking if it's J is not equal to I So that's the index make sure they're different indexes right because I can't just pick the same number add them again, right? They have to be distinct So that's what this is for Okay, I make sure that my current number is less than M because If it's greater than M then if it if the number that I find is greater than M So like let's say this was let's say this was four, right? I don't want to replace four plus three Right because I don't want to replace actually this is a bad example Let's say this was five Okay, so let's say this was let's say this was five and Let's say my M is equal to four Right if I find a number that's greater so five And I find another number that's greater so like actually let's say this was Let's say this was five So if I find a number that's great and another number that's greater and I try to replace this I don't want to replace it with a number. That's also greater Right think greater than my M Because what I'm trying to do is make them all less than or equal to M So if I find a number that's greater, I don't want to replace sum them up and replace it So yeah, that's why I have that so in the here. I make sure that a of J is less than M Okay, so once I find another number that's S and M I loop again from the beginning and I make sure that I find a Last number that's less than M So this last number has to be less than M Right, I find another number. That's not the same thing as the original first two and I make sure it's less than M Also if K and that's my K and I make sure all them are not the same indexes because they have to be different numbers, okay? Then what I do as I check if the sum of my both of these two numbers I just found is less than or equal to M and if it is then I just replace my A of I is equal to A of K Plus AFJ, so I just replace them Okay, I have a Boolean that is just Finished and this finished is just to end the loop when I'm done So if I do replace this what I want to do is break out of this the second loop of finding the second number, right? But I want to continue going on the first loop because this first loop is What the loop that is replacing all of the original numbers, right? Because I want to replace every single number to see if I could replace them with a number that's less than or equal to M Right, so that's why I break I have a finished For the second loop that breaks out of the second loop. So in this case Originally, I say finish is false Then I do this loop for loop and I check if it's not finished, right? If it's not finished So in the end I do if I did replace it. I say finish is equal to true and I break So that breaks out of the second break This breaks out the first loop and because I say finish equals to true the second loop is going to be finished, right? Because this true Finish is true now and not true is false. So this second loop is done So this this second loop is done and then it'll go to the original first loop and it tries to replace the rest of the numbers of that So yeah, that's my solution. I think Their solution I think is they use sorting and I think you could do that and that might be a better way But I'm not a hundred percent sure Let's see Because I just brute-forced it. Okay, let's look at theirs. Oh wow. So Since they're all positive AI plus AJ is gonna be greater than max AI JJ That means you can't make the first Second numbers lower than all their RDA. We choose any other number of place. We can't make them less than Make them bigger as for some means we just choose each of them either not to change it or make it equal to Mn plus Mn So just be able to make that all of you just have to check that's not equal to D Well, that's kind of what I did M plus N less than equal to D. Okay We could do it by sorting our rate in decreasing order and checking that either AI plus a 22 is less than or equal to D. I Mean, yeah, you could do that. I guess. Oh Wow All you have to do is just sort it so we're gonna check that's not less than or equal to D a 0 plus a 1 is less than or equal to D Well, the last one is less than or equal to D Huh That's cool to know But yeah, what I did was I just brute-forced it Rate comps describe. I hope you guys enjoy my explanation Basically, I just took two numbers and replaced it and then I break out of it It's my way is not that difficult to understand I Could explain it again, but yeah Just I'll just explain it again. So go loop through it I Find if you find a number that's greater than the M the value of M that they want Then what I do is I create an I loop through it again Find a J that's not equal to I and I make sure that a of J is less than M If it is less than M I loop through again from the beginning and I find another another number that a K is Not equal to J and K is not equal to I make sure they're all distinct in there also less than M And then if they are then I add them up and I check if it's less than or equal to M And if it is then I set it I replace it and then I finish just your and I break So yeah, I Hope you guys enjoy this video. It's not that long Plus I didn't really feel like that much editing today because I have a lot of work I have to do But yeah, rate comps describe. I hope you guys enjoy this video. I'll check you guys later. Peace