 So I haven't had a chance to do this in a long, long time. Sorry, guys, I just didn't have time to make vids. But here we go. We're going to get back into this. So one of these pattern that appears a lot in coding interviews is the cycle sort. And this is actually not taught at all in most of the algorithm classes in college. At least I wasn't taught about this, but essentially is that what we're going to try to do is we're going to try to create cycles in the array. And then we're going to try to swap them from the cycles into their corresponding proper position. Okay, so that's legit how it is. Um, it is optimal in the number of memory writes. So like writing down the certain memory, like doing the number of swaps, that's optional. So that's the good part about it, but it is unstable. Okay. So it's unstable. So that's the bad part about this sort. And surprisingly, this is not taught at all in school, but that's the good part about it is that now we can learn it more when I do coding interviews. But yeah, when we say unstable, it means like, let's say we had two, two, right? The first two is not going to be at the right spot. That many a number of times. Okay. So it might be on the second spot when you sort it again. Right. So that's what it means by it's unstable. Now let's get into how this works. All right, guys. So let's go over this problem. Missing number. So given an array numbers and containing and distinct numbers from zero to n, we need to return only the number that is missing from the array. So when you see this three zero one, right, this array of three zero one on the number two is missing from this array, right? Zero one, two, that's missing from this array. So in this case, we're going to output two because it's missing from this array, right? We're given an array of numbers containing values from zero to n and we need to return the value that is missing from the array. So yeah, here in here, n is equal to three because of three numbers. So all numbers are in the range of zero to three, but two is missing in the range from zero to three. So therefore it does not appear in here. So we're going to return two. Okay. Now let's look at this one. All right. Numbers zero one. All right. So here in this case, example two is that n is equal to two because there's two numbers. There's two numbers here, right? Zero and one. There's two numbers and we need to find the missing number in the array of zero up to two. So now in this case, two is missing in this array because since there's two numbers, all the numbers are in the range of zero to two, but there is no two in this array, right? There's no two in nums. So therefore two is a missing number since it does not appear in nums. Okay. So that's example two and example three. Now if we were to list all the numbers, zero, one, two, three, five, six, seven, eight, nine, we're missing eight. Okay. Right. These numbers nine, six, four, two, three, five, seven, zero, one, right out of these numbers that are from the range zero to nine, we are missing eight. Okay. If we were to list them out from zero, one, two, three, four, five, six, seven, eight, nine, 10. Oh, not 10. Zero, one, two, three, five, six, seven, eight, nine. We're missing eight. Okay. The values from zero to n here in this array, we are missing eight. Okay. So yeah, since there are nine numbers and they're on the range from zero to nine, eight is missing in this nums. Okay. In the range, it does not appear in here. Number eight does not appear in here. Therefore it is missing. So we return it. Okay. So that's the gist of this question. So now how do we dissolve this problem? Okay. So going back to the coding interview pattern. Okay. In the cyclic sort, what we're doing is we're going to try to put the number zero at the index zero, then we place the number one at index one and so on and so forth. Okay. So since all the numbers are unique, if we place each number at the corresponding index in the array, we'll be able to find the proper way and find the index that does not have the right number. Okay. So in this case, let's say we have the rate of zero. This array four zero three one. Okay. So what we're going to do here is we're going to place the number four at its corresponding spot of four. Okay. That's what we're going to do. But in this case, we're going to ignore the number and the last number and because we can't place in the array. Okay. So in this case, we have number and numbers, right? Zero one, two, three, four, right? We can't place the last number in the array otherwise. So we're just going to ignore it. Okay. So in like, you know how in our previous examples here, right? We have zero one, two, three. This three that has to be at the end. If we were end up sorting it, but we're going to not we're going to have to ignore it for now because this is the largest number, right? So yeah, okay. So since the array starts with n numbers, the numbers go from zero to n minus one. So in this case, there's four numbers, right? Zero one, two, three, four. So we go up to zero to three. Okay. So those are the numbers we're going to try to put them in the right spot. Zero one, two, and three. Okay. We're going to ignore the last number four because technically we can't even put it at the right spot in this array. Anyway, so yeah. So here where we start out is four is the number n. So we're just going to ignore it, right? It doesn't belong in the array, right? Because we're going to ignore it. It's going to be in the last position, right? So we're just going to ignore it for now. Okay. Now we get to the number zero. Okay. Zero, what is zero? We need to put zero at the proper location of zero of the index zero. So what do we do? We swap it with whatever is at the zero index. Okay. So we are at zero. We're going to swap it with whatever at zero. So now this zero is at the right spot. Zero is now at the zero index. You see this? So therefore this is zero is at the right spot. Okay. Now we can go to the next one. Okay. So now zero is at the right spot at the zero index. So now four is the number n. We're going to ignore it. Okay. We're going to ignore four for now because the last four is going to be the last number. It's the largest one. So it's going to we're going to ignore it. Okay. Now now we're at three. Three is not in the right spot. Right. Three three is at index zero one two right now. So what we're going to do is we're going to swap three with the actual number at the third index. So what is the third index zero one two three. Right. So here we're going to swap three with the third index of whatever is at third index. So in this case one is at the third index. So we're going to swap with three with one. Okay. So now three is in the proper position. Right. So now I have zero four one and three. So three is in the proper position. Okay. So now we're going to go back. We're going to come back here and now we see that one is not in the right spot. So we have zero one two three. I was looking through one is not in the right spot. So we're going to have this one here. He needs to go in the position of the index one. Right. So we have zero one. So this one this this one at the second party or has to be at the position of the index one. And since this one is not in the index one, we're going to swap it with whatever is at index one. So in this case one is going to get swapped with four because four is that index one. So now one is at the right spot. So now we have zero one four and three. So now now the number four is at and right and we're just going to ignore it. So now we have zero one four and three and we know three is at the right spot and we know one is at the right spot and we know zero is at the right spot. So only four is not at the right spot. So as we're iterating through the array, we could just find out which index is not equal to the current number whatever number at the position and then that will just be the run that's not in the right spot and then we just return that. But here's the code how to do it. Okay, so we start with this missing number here. Okay, our index starts at zero while our starting value is less than our total number of elements. We're going to loop through from we're going to get a current number right current number for starting it and if it's less than the number of elements right and it's not equal to it. What we're going to do here is this Python thing is going to swap it. So we're going to swap it at the corresponding positions and then we're going to increment our index. That's what we're doing. Okay, so this is going to just swap it in place with whichever value that is not at the right spot. And in the end of all the values that are not at the right spot will be at the right spot besides one right because we're going to find the one missing number. So in the end, there's going to be one missing number that is not at the right spot. Okay, and then after that, all we do is we're going to loop through from the beginning to the end and we're going to if it's not equal to the number at the right spot of our current index not equal to the current current number, then we're just going to return it right. We're going to return it and then that's it right in the end. We're just going to return the length the numbers of everything is at the right spot. If all the numbers are at the right spot, we're just going to return that. Okay, so yeah, this is a time complexity is o of n plus o of n minus one, which is o of n and this runs in constant space. So how do we identify this anything with dealing with duplicates or in a range? You probably cyclic sort. So if you're trying to find anything numbers in a given range cyclic sort is the way to go. Okay, so let's go over some more problems now. All right guys, we're going to do another problem. Find all duplicates in an array given an integer array of numbers length n where all the integers of the numbers are in the range one to n each number appears once or twice return an array of all the numbers that appear twice. Okay, so we need to return all the numbers that appear twice. We know that each number appears once or twice. Okay, so how do we know that this is a cyclic sort? Because we know that all the numbers here are in the range between one to n. If this case was never the case, don't think about using cyclic sort because you cannot sort it in the right position. If there if not all the numbers are between one and n, you're going to get an index out of bounds. Okay, so the only reason why this cyclic sort works is because we know given a fact that precondition all the numbers in numbers in this array are between one to n. Okay, given an array of length n all the numbers are appear between one and that is the only reason why we could do this. Okay, so don't think that you could use cyclic sort every time. Only reason why is because of this precondition. Okay, so now let's look at this. We need to find the number return all the array of numbers that appear twice. So here look at nums. Okay, let's look at nums. Nums we have in value of values of all these values all these values here right 43278231 which numbers appear twice to two appears twice like we have one to and another to two appears twice. That's going to be an array. What about three three look at three three and three three appears twice. We see this one three and we see another three. That has to be an array. What about four four only appears once. What about five? There is no five. What about six? There's no six. What about seven? Seven only appears once. What about eight? Eight only appears once. What about one? What only appears once? So the only two numbers that appear twice are two and three. So we're going to return two and three. Okay, simple example to nums are between one one two. Okay, this array of numbers one one two which number appears twice one. It appears twice. Why? Because one there's one one here and there's another one here appears twice. We return one but what about two two only appears once so we can't just return it because to have to appear twice we need the array of numbers that appear twice and because one only appears twice we're going to return one. What about this nums is one. No number appears twice. Do you see any number here that appears twice? None. We return empty. So that is how this problem works. That is this problem. Now let's go over the solution. Now I personally didn't use in cyclic sort but if you use cyclic sort it is much much easier. So let's actually go over cyclic sort. Okay, so here is a Java solution to bear with me. This swap function swap just swaps it at the corresponding spot. So if if the index at index i is going to get swapped with the corresponding right position. Alright, so temperature error is going to equal to i our current numbers i is going to equal to whatever new number and then our current number new number at that position at i is going to equal to the temporary. So this swap just swaps it at the right spot. Okay, so that's this this helper function but the beer the main gist of the problem here is fine duplicates. So here what do we do? We start at the beginning zero and we're looped to the end. Okay, and this is at the last value of our array. So here the index. What is the index? We need to take a current number and we minus one. Okay, so let's say we have at like, um, three, right? Let's say where our numbers three. What do we do? We're going to swap three with that whatever corresponding spot of three, but whatever value at three is going to be at index two, right? So that's why we have this thing. We're going to take the number at i minus one. If this number is not at the right spot, we swap with it. Then we increment i plus plus. Okay, so this loop is going to continually going forward until we all the values are swapped at its corresponding right spot. Now what do we do? We loop through this array and we start at the beginning index zero to the end. And what do we do? If the current index is not equal to the index plus one, so if our current number one is not equal to one, right? Our current number one is not equal to one. We're going to add it to it. Is my current number two is not equal to two. We're going to add it to it. Current number three is not equal to three. We're going to add it to it. Okay. So what this is doing is that when we put everything at the right spot, we're going to have duplicates are not in the right position. Those values are not in the right position are going to be the duplicates. So if they're not in the right spot, we add it to our new array of lists and then we return it. So whatever value that is at not at the right spot is going to be a duplicate. Then we return our array of duplicates. So I could go over an example very, very quickly. Let's go over example using this idea. 4, 3, 2, 7, 8, 2, 3, 1. So let's take this number and let's put this in our let's put let's put in paint. Okay. Let's put in paint. Let's go over example. How this code will work. So I showed you how the code works. The pseudo code for it. Let's let's go over an example. Okay. So let's go over the example of this. Okay. So we're going to have this 4, 3, 2, 7, 8, 2, 3, 1. This four, right? We're going to put it at swap it at the right spot. So which is what is that? What should this four be at? Well, since all the values are indexed from starting from 1, right? So since because it's based on this precondition, all the numbers are starting from range 1 to n, right? So this 4 is going to be actually be at index 3, right? Because let's say we sorted all the values. 1, 2, 3, 4, 5, 6, 7, 8, right? Let's say we sorted all these values. This 4, right? Since because it's started at 1, this 4 is actually going to be at index 3. Let's see, 0, 1, 2, 3. So this number, what we're doing is we're going to actually swap it at the current value minus 1. Okay. So 4 is going to get swapped with index 3. So what is that index 3? 0, 1, 2, 3. Whatever value at 7, we're going to swap it with the 4. So what this is going to end up happening is that we're going to have, I'm going to hear it. So this 7 is going to swap with 4. So then we have 3 and 2 and 4. And then the rest are going to be the same. Right? That's it. That's it. Okay. So now this 4 is at the right spot. Okay? So now we're at 3. What is 3? 3 is not at the right spot. So we're going to swap it with whatever is at the index 2. So what is that index 2? 0, 1, 2. This 2 is at index 2. So we're going to swap 3 with 2. So we're going to swap it with that. So this is going to 7, 2, wait, 0, 1, 2, 2, 3, 4, 8, 2, 3, 1. Okay? So now this 3 is swapped with that 0, 3 at the corresponding value of index 2. So now we have 7, 2, 3, 4, 8, 2, 3, 1. Okay? We'll out this 3. 3 is at the right spot. 0, 1, 2, 3. Yeah. 4. 4 is 0, 1, 2, 3. 4 is at index 4 minus 1. So yeah, this is at the right spot. 5. So it's 8 at the right spot of it. Well, first of all, is 8 equal to our last number, n. Let's see. 1, 2, 3, 4, 5, 6, 7, 8. It is. So we're actually going to ignore it. We're going to ignore 8 for now. But actually, let's see. 0, 1, 2, 3, 4, 5, 6, 7. Actually, we're going to swap it with 7. This 8 has to be at the end, right? So we're going to swap it with whatever value at the 7. So we're going to swap this 8 with this last value at 7. So this is going to be 7, 2, 3, 4. So what is the last value at 7? 1. So we're going to swap it with 1. So 1, 2, 3, 8. Okay, so 8 is at the right spot. What about this 1? 1 is not at the right spot. So we're going to swap it with whatever value is at the first value here. So we're going to swap it with there. We're going to do 1, 2, 3, 4, then 7. Right 7 was at 1. This spot runs. So we're going to swap it with this 1. So this is going to be 1, 7, and 2, 3, 8. All right, what about this 2? 2 is not at the right spot. So we're going to swap it with whatever is the value at 2. So we're going to swap it with this 3. So this is going to be 1, 2, 2, 4, 7, 2. Let's do this 3. Hold up. Yeah, so this 2 gets swapped with this 3. So this is going to be 3, 3, 8. Okay. Okay, and then this 3 is going to not at the right spot. So we're going to swap it with 4, right? So this 3, let's see, 0, 1, 2, 3, 4, 5, 6, 6, 6. Okay, so this is here. 3 is not at the right spot. We're going to swap it with 4. So we're going to 0, 1, 2, 3. So this 3 is going to swap with this 4. 0, 1, 2, 3, wait, hold up. 0, 1, 2, 3, 2. Yeah, okay. So yeah, this 3 is going to swap with this 4. 0, 1, 2. Actually, no, this 3 is going to swap with this 2. What am I doing? 1, 2, 3, 4, 7, 3, 2, 8. And this 2 is going to get swapped with, yeah, okay. So after 3, and then 8 is at the right spot. Okay, so at this point, all the values are in the right spot. Besides the ones that shouldn't be in it. Okay, so we have 1, 2, 3, 4, 7, 3, 2, 8. And then what we're going to do is we're going to loop through this a right and then whichever value that is not at the corresponding right spot, that value is going to be duplicated. So let's see. I feel like I'm just missing something. We know we didn't finish this. Yeah, okay. Yeah, so this value should be duplicated. Let's see 0, 1, 2, 3, 4. This should be a 5. So that's not right. 6, 7, 8. So 8 is right. 7 should be here. So this should be swapped with that, I think. So this 3, 4, 2, 3, 7, 8. Okay. Okay, so let's see. 1, 2, 3, 4, 2, 3, 7, 8. These values should be at the right spot. So this is at the right spot. Whichever value is not at the right spot is going to be our duplicate. So here 1 is at right, 2 is right, 3 is right, 4 is right. 5 is not equal to 2. So we're going to add 2 to our new array. 3 is not at the right spot. So we're going to add 3 to our new array. So this is 4, 5, 6. This is the right spot. 7 is at the right spot. 8 is at the right spot. In the end, we just return 2, 3. So yeah, that's the gist of how this code works. Keep swapping it without the right spot and the corresponding value in the array. And then in the end, whichever is not at the right spot, that'll be the duplicate number. So we return 2, 3. So yeah, that's how this code works. And let's go over another problem. Second problem. Find all numbers disappeared in an array. Given an array of numbers where numbers i is in the range 1 to n, return an array of all the integers that do not appear in numbers. All the numbers in the range 1 to n that do not appear in numbers. So we have here 4, 3, 2, 7, 8, 2, 3, 1. Okay. 5 and 6 do not appear in this array. Between 1 and n, all these values are between 1 and n. 5 and 6 are not appearing in numbers. So what we're going to do is that we're just going to do nothing. Right? I'm joking. We're going to return 5 and 6. Okay. These values don't appear in here. So we return 5 and 6. 1, 2. 1, 1. 2 does not appear in this values between 1 and 2. So we return 2. Okay. So all the numbers that are not in the range 1 to n that do not appear in the array. We got to return the array of it. So let's just go over the solution really quick because all you need to do here is do the exact same thing of what we did before. So, right here. Right here. Okay. So we start from 1 to n. Here, they do it differently here. They actually make it negative. They mark it as negative, which is also another thing you could do. You could mark it as negative. That's pretty good. So here we have values of n, right here. Okay. So n is a number of values. Result is what we're going to return. All right. So here we start i equals 0 up to n, i plus plus. This is in the index that we're going to swap it with. Okay. That's where we swap it with. Noms at index is going to equal to, so if this case is greater than 0, we're going to make it negative. Otherwise we keep it as its right spot. So here in this situation, they didn't actually swap anything, which I think was kind of dumb, but instead they negate it, right? Whichever has the right spot, they're going to negate it. So you could do it this way. Actually, there's no, there's no issue with it. I just think it's better to swap it to follow the same pattern. But yeah, here instead of swapping, they just made it negative. Okay. If it's greater than 0, we make it negative. If it's already less than 0, we keep it as its own. In the end, all the values that are greater than 0, they're missing. They're missing. So we're going to return, we add i plus 1, the proper value, the right corresponding value, and then return it. So yeah, here this is what they do. They just like mark the negative, whatever, the right spot negative. That's what they do. So here in 8, 2, in the end, we have 8, 2. And then because they're indexed in the right spot, 5 and 6, 5 and 6 are not in this array. And instead they have 8 and 2. So in the end, they just return 5 and 6, because that's at the index of 5 and 6. But yeah, that's what they do. Yeah, so that's this code. And then let's go over the last value and then we'll be on our way. So here, we have a set of integers s and it contains all numbers from one end. Unfortunately, some numbers got duplicated in another set, which results in repetition one number and a loss of another number. Actually, one of the numbers got duplicated in another set. So you're given numbers, array of numbers, nums, and then we need to find the number that occurs twice and the number that is missing or return in the array. So in this case, example 1, 1, 2, 2, 4. 2 appears twice and the number 3 is missing. So we return 2 and 3. In this case, 1 appears twice and the number 2 is missing in this array of nums between 1 to 2, right? So we're going to return 1, 2. So yeah, that's the gist of this. And let's just go directly to the discussion of how to do this in this case. So in this case, they didn't swap it. I don't know why, but it's the same technique though. Instead of what they're doing is they're going to set it as a negative 1, okay? So here, duplicate is equal to minus 1. They have a variable for that. Missing number is equal to minus 1, which is just like a variable for that also. In the case, they're both negative 1 in the beginning because they're just random placeholders for now. We loop through from 0 to n and we get our current number. This current number is going to be the index and we make it the absolute value, okay? If it's less than 0, right? That means we already marked it as negative already. So this must be the duplicate number because in the problem, it said that duplicate number is 1 duplicate number and we need to get that. So that must be the duplicate number. So yeah, so if we already marked it as negative, then we're going to set it the duplicate equal to the number. Otherwise, we're just going to mark it as negative. So whichever at the corresponding position, num minus 1, we're going to mark it as negative. So basically what we're doing is we're looping through the array and at the corresponding right spot, we mark it as negative. Then in the end, whichever, if we already seen it as negative, the one that is negative that we've seen twice is going to be the duplicate one. So yeah, and then here we're going to loop through it again and whichever value is greater than 0, right? That means that it wasn't marked as negative. So that number must be missing. That is a number that's missing from our list, right? So basically whichever number is a marked duplicate, okay so the first array is looping through it and it's going to mark the corresponding index at the right spot to be negative, right? At those spots. If we already marked it twice, that's going to be the duplicate number, right? If it's already negative, then the duplicate number is going to be the current number, right? Because we've seen it twice. Because we're marking all the values to be negative. Now here is that if there's a number that wasn't marked that is already positive, then that's the missing number, right? Because we're basically what we're doing is at the right spot. So let's say we have 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, right? Let's say it was like 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Let's say there's a random number that's missing, let's say like, I don't know, 18. 18 is missing, okay? So since we're going through and we're marking at the right spot, if the current value that is greater than 0, that means that we never marked it before. So that's the missing number. So we're going to set missing is equal to i plus 1, okay? The first loop is just going to loop through and mark all the values that are in the correct spot, in the right spot is going to mark it as negative. If we've seen it twice, if it's already negative and we've seen it twice, that must be the duplicate. If we never marked it at all, that must be the missing number. So if we never marked it as negative, it's all positive zeros. That must be the missing number, so we break. In the end, we return the duplicate number and the missing number. That's the gist of this idea. We could actually go through an example of how this would work. So we have 1, 2, 2, 3, 4. So let's actually go over an example of how this would work and then we'll be on our way. So 1, 2, 2, 4, 1, right? At index 0, we're going to mark it as minus 1 at the right spot. So here, 1 is going to be at index 0, so we're going to mark this as minus 1 and make it negative, okay? 2, right spot is going to be at index 1, which is at the right spot. We're going to make it negative, minus 2. 2, 0, 1, 2, right? This should be at the right spot of minus 2. Since this value is already negative, this 2 must be the duplicate number. So we're going to mark it. Duplicate is going to be 2, so our number, let's say our duplicate number is going to equal to 2. Because this is already negative, okay? Now 4 is 4 marked, oh wait, 4 at the right spot, 0, 1, 2, 3. 4 is not at the right spot. But we're going to mark 3 as negative, so 0, 1, 2, 3 is going to become negative. So here, we're going to have, this is going to become negative, so this is going to be, man, I can't edit this. That sucks, okay? So here, it'll be minus 1, minus 2, 2 stays there. 4 becomes negative, right spot, becomes minus 4, okay? Now we loop through this again, whichever is greater than 0 is going to be, let's see, 0, 0, 4, 0, 1, 2, 3. Okay, so whichever is greater than 0 is going to be the, hold up, let me just go back really quick. Making sure this is right. Oh yeah, whichever is greater than 0, that'll be the missing number. So I plus 1 would be the missing number. So in our case, here our missing number would actually be 3. Because this number 2 is greater than 0, so this would become 3, so our missing number is actually 3. So 0, 1, 2, 3, yeah, so this becomes, um, missing number 2 is greater than 0, the missing number would be 3. So here, missing would equal to 3. And yeah, then we return 2 and 3. So that's the gist of this code. I hope you guys enjoyed this video. Right, comment, subscribe, I'll check you guys later.