 What's up guys, my name is Michael and welcome to my YouTube channel today We're gonna do another lead code challenge. This is called relative sort array So you're given two arrays one is a one a two All the values of two are distinct all elements are two of array two are in array one Sort the elements of array one such that the ordering from a one is the same as the ordering of a two Elements that don't appear in a two should be placed at the end in increasing order So based on this example, we have two array one has two three one three two four Six seven nine two nineteen as you could see array two has two one four three nine six two one four three nine six These elements of array two are in array one and what they want us to do is return the output array such that These the elements that that are in array two are in the relative order So you see all the values of two one four three nine six are the same ordering as array two except that These values are the values of array one that were in the relative order All the values after this Six seven nineteen are the rest of the values in of array one that are not in array two and They are sorted in increasing order so I'm going to explain my solution and why my solution is actually not as efficient as What you normally could have so what I did was I had a Vector called to return first. I went through all the values of array two Then I went through all the values of array one and then for every value of array two and array one if they are equal I add that Array one value to my to return array so that's what That ensures that it's in the right pattern two one four three nine six and These get these values get repeated basically these are these are the right ordering is the right ordering then I set array of J is equal to negative one that means that whatever value that I looked at in array one Whatever value I looked at two one four three nine six is are going to have Negative one as their values. Okay, so that means that they're already seen. They're already looked at okay Then I sorted my array one to ensure that the rest of the values aren't us increasing order Ascending order, so that's for six seven nine six seven nineteen To make sure that's in an increasing order Then I went through through the array again, and then I check if I haven't looked at it Then I add the rest of the values into the array that I'm returning So I add six seven nineteen into my to return array and then I return to return so essentially this is this ensures that the values in here are in the right relative order because every time we see the right value of Array at The first array is equal to the second array it ensures that we add the first whatever first value of the first array Okay, so ray one We add that value. So if it's equal to array two like two one four three nine six I'm going through and I checking this is equal to two Does it okay add it to my new array does equal to two no does equal to does equal to equals the two added again So that makes two two Here does equal to two no no no no no goes here. Okay? Was the two does it adds to again here another output array is equal to no Then I keep going and say check one does equal to one does equal to one one one one one then I add Oh, I found one one Does equal to four I add all the values that you equal to four into my new array It's equal to three all values of three in this case three three Add that into my array Add all the values of nine all the values of six and then after that I sort it Set those eyes a negative one and I add whatever that wasn't visited means that it's in the I'm supposed to appear in the end of the ray. So I pushed those back last Okay, so now let's look at other people's solutions. Let's see. Mm-hmm Okay, so this guy had a map Okay, this seems more reasonable actually so he had a map. Oh, so he kept track every value of For all values of ray one he he kept track the number occurrences that ray one occurred. Okay Then he had a position of zero, okay For you went through every value of array two. Oh So then okay, so For every value of ray two For all the occurrences of ray one He decreases those while they're greater than zero and then he adds those values to Array one at the position is equal to value of ray two Then he goes through the rest of the array of Wait, what is he doing? Okay, he goes through the Okay, so he goes to the currents list again. Okay while Okay, so he iterates through the So he okay, so he iterates through every value in the occurrence occurrences again and Then he subtracts every occurrence while the occur in the number of currencies for for each element of ray one is greater than zero so he subtracts those and Then he adds Ray one it dot first. Okay, so he adds the first element Huh I don't know how this works Why does it? Why does he do this part? Right. Oh Because in all these okay all these values. Okay, so all the ones that are repeated All the values of array two that are repeated array one they get added first. Oh So that's what this is ensuring. Okay for all values of array two They get repeated so they they get added first Okay, and then he adds the rest of the values That's what this is this code line is doing while it that second minus minus greater than zero. This is the rest of the values Okay So we could implement the solution Pretty easily. So all we have to do is go through all the values Okay, so we have to We have to keep track of all the values of ray one and we have to map the number of occurrences for a one Then we have to go through all the values array two and then while the The we got to keep subtracting the values of While the number of occurrences for the values of in our occurrence array is greater than zero He keeps subtracting that and then we're gonna add that to our array and Then we add the rest of the values to our array. Okay, so Okay, so we could do that. This is pretty easy. Okay, so We are going to map This is array two occurrences. Okay So We're gonna go through every value of Oh, no array one occurrences my bad ray one occur ray one size I plus plus Ray one occur I I Plus plus So this ensures that we're adding all the number of occurrences that these values of array one appear So two would have three one's gonna have One four is gonna have one Who's gonna have two values nine is gonna have one six gonna have one seven is gonna have one 90 is gonna have one Okay, now we're gonna go through all the values array two Okay then while The For every value of array two While the occurrences of array one at array two is greater than zero We're going to subtract it and then we're gonna set our position of array one to equal to that Okay, so Int position is gonna equal to zero Well, oh, so while Ray one occurrence at array two at X It's greater than zero we are going to subtract Subtract one every time for in our occurrence list so the reason why we do this is because array two all the values array two are in array one, so essentially we could access How many times have to showed up? Okay, the number of times two is greater than zero we're gonna How many times of one showed up how many times before showed up how many times three showed up how many times nine I showed up how many times six showed up while that That number of times showed up in our occurrence Mapping of array one occurrences We're gonna decrease that count and then we're gonna add that number of times into our new our new array So we're gonna replace those values of array one Array one at position plus plus is gonna equal to array two at X Okay, that's what this is gonna do After that we need to do that Okay, so now once that occurs we need to do Let me think let me think what do we have to do we have to go through every map again and while The array one occurs greater than zero We have to decrease it and then we add the Rest of the values, which is dot first. Okay of our occurrence to Array one at position plus plus so we're gonna do that. So For every Auto it iterated at array one occur we're gonna do while Dot first Wait, what did he do? Let me see. Let me go back quickly go quickly go back Hold up. Hold up. Hold up. This one. I think yeah. Okay. So. Oh, yeah, so the rest of the values same Equal to first. Okay, so that's what we have to do so while the the rest of the values so while the Rest of the values the number of occurrences of our map then the rest of them is greater than zero We are going to add it to our Array one position Is gonna equal to it dot first And then after while we're doing that we decrease our second count So I'll explain that again Then he returns return array one That's on the code So I'm gonna explain to you how this code works We keep track of all the values in a map the number of occurrences for each L a value of array one so in this case There's two three three twos one three and four One one two threes one nine One six one seven one nineteen right that's what this is doing They've this keeps this keeps track of the occurrences of array one We're gonna go through them and count the occurrences of all the values of ray one. That's what this is doing Wait, I my bad So that's what this loop is doing is a position We keep track of the value we're currently adding to the array for all the values array two While it's number of occurrences in our occurrence list is greater than zero We are going to add those values Okay, and we're gonna subtract number of occurrences that array two showed up in our list of numbers that it shows up So essentially this is gonna add and we're gonna add those values So it's gonna add Then the number of times two showed up number times one showed up No times four showed up This case for number times three showed up three showed up twice So it adds those number times nine showed up was once so adds that and that's what this is doing That's what this for loop and while loop is doing. It's adding those values here Okay, and then we want to add it the rest of the values So that's what this is doing add six seven nineteen because it's checking the the last two values While it's the number of current the rest of the values number occurrences is greater than zero then it's Adding those values because by the time this loop runs and it's already done It's going to add all these values of two one four three nine six Those number of occurrences in our occurrence map is going to be zero So then the rest of the values are gonna have at least just have one occurrences one occurrence We're just six seven nineteen and those get added last. Okay, so that's what this this loop is doing So yeah, great calm subscribe. This is the better solution. I hope you guys understand my understood my Logic of what I explained. I'll take you guys later