 What's up YouTube? Mike the Coda here. Today we're going to go over the next coding interview pattern and that's fast and slow patterns. So at the previous two, previous two videos, we talked about the sliding window approach and the two pointer approach. So now today we're going to just talk about fast and slow pointers. So what this means is that you're, what we're doing is that one pointer is going to move at different speeds and the other pointer they're going to eventually meet eventually. So that what means is that one pointer is going to go super fast and the other one's going to go slow. And then the end where they're going to meet sooner or later, they're going to meet. Okay, so what is a good example of this? The first one is linked list cycle. So given a linked list, determine if it has a cycle in it. So a linked list is like this, where each of the node represents a number, and it's pointing to another node that has another number. Okay, so basically, they're all the elements are like circles, and they have a certain number inside of it. So let's say we have input head equals three two zero negative four, and the position is one. The link list just represents like this three two zero negative four. Okay, and our problem is that given a linked list, we have to determine if it has a cycle. So what that means is that if there's a loop inside, right? So if we have three two zero four negative four, are there loops in these pointers that we're connecting towards each other. So in this case, there is a loop because the two goes to zero and the zero goes negative four and the negative four goes back to two. So in this case, we have a loop because there's a cycle here. Okay. So yeah, this output is true. And they want to give us a linked list, right? And they give us a position and the position represents a position of linked list where the tail connects to. So yeah, so there is the tail connects to the second node here in this case. So this negative four points back to two. And in this scenario, we use a position representing zero index where the last node of negative four here, negative four connects back to position one. And position one, if we represent these nodes by zero base, we have three would be zero one two three, right? If I just start counting from zero, I would have zero one two three if I'm counting from zero. And since they say position one, zero one, this second position is a two. And the negative four, since they said the position represents the position where the tail connects back to negative four points back to position one, which is zero one, which is two. So negative four connects back to two. So if there's a cycle on the link list, the tail connects to the second node. Okay. So yeah, and then here, there's another example, we have one two, and the position is zero. So in this case, the two connects back to position zero. So zero one, and then this two connects back to zero. So it's back to position one. So there's a cycle on this link list because the two goes back to one. So that's a cycle. Okay. And then here, position is negative one. And a head is one, and it outputs false because there's no cycle on the link list. Okay, so how do you do this problem? Essentially is that, imagine there we have a turtle racetrack, and then one is going to go way slower behind the other one. So we have two pointers, one is going to be the slow pointer, the next one's going to be the fast pointer called the hair. So if they long, if the link list does not have a cycle in it, the hair is going to reach back to the link list where the slow pointer, the turtle is, is where it's at. So that means that there is no cycle in the link list. And the turtle will never catch up to the hair. So the slow pointer will never catch up to the fast pointer if there is a no cycle in the link list. Okay. So if at any stage, the turtle's slow pointer meet the hair fast pointer, we can say that there's a cycle. Okay. So in this case, we could look in this picture example. So we have this one, and then the two, and then the hairs are going twice as fast as the turtle. And then sooner or later, once we go back towards the end, they're going to meet, right. And when they meet, that means that we are if they meet, that means that the slow pointer reaches a fast pointer and that means they're at the same scenario. Okay, so that's what this means. Okay, so this is like how you do it in Python. Okay. And yeah, now let's go over and another problem, which is like, given a linkless return the node where the cycle begins, if there is no cycle return all. So in this case, we have zero to zero negative four and position is one, the tail connects back to position one. So we have zero one two three, negative four connects back to zero one, which is the position of one, which is two, so negative four connects to two. Okay. So now given a linkless return the node where the cycle begins. So we have to return the position of two. So yeah, we use integer position representing the linkless with account tail connects to okay. And here. So yeah, here in this case, the node connects back to index one. So the tail connects back to index one. So the output would be one here. Okay, now here we have the second example, we have one two, and then the position of two connects back to position one, right, because the starting position zero zero one, and this two connects back to one, right, because they stated that the position is the position was where the last node connects back to the first node. So you have zero one, so two connects back to position zero, which is one in this case. So there's a loop here. And there's a cycle in this necklace. Okay. And here we have one and then positions I get one. So the last node does not connect to anything. And there's no cycle. Okay, so how do you solve this problem? It's not very difficult. So here, we're just going to have the slow pointer pointing from fast, fast is going to start at. So we have two of these. Essentially, we have two of this. Two of the pointers are going to point to the starting node. And then one is going to go twice as fast as the other. So while, while the first node is not null, and the second one and the fast next is not null. So what this means is that first, they didn't go to the end yet. So that while the fast pointer is not reached the end and the next pointer of the fast point is not then the fast one is going to go twice as fast. And the slow one just goes to the next one. So the fast one is going to go next to next. So it goes twice as fast and the slow goes next one. If the slow reaches the fast, what we're going to do is we're just going to set the current is equal to the head, right? And then if the current is not note is not the slow one, then we're just this is just a scenario you said, just set the current to the next one, and then the slow is going to equal to the next pointer. And then in the end, we just return slow. So essentially, is that this this while loop is just in case if we reach a scenario where we have to go to the next node of the slows next node, right, when current is not slow. Essentially, is they're saying that we need to we just have to like update it at that point. So yeah, and then we return slow. And in the end, we if none of this occurred, we just return none. So yeah, so the only difference between the two problems is that this part if the current, if we reached, if the slow point is equal to the fast pointer, we're going to meet at the same location, right? And if that's the case, the current is not slow, while the current is not slow, we just go to the next current. And then slow is equal to the next one that returns slow. So this is how it works. This is the diagram. So here. Yeah, so we start from here at the head, and then we go to the next pointer. And then the hair, the fast point is going to go twice as fast. So this one would jump to here, right? So the bunny, the hair, the fast pointer is going to jump to this the second one. And the turtle starts with the head. When the turtle goes the next one, the fast point is going to jump twice. So we have one, two, and it jumps back to here, right? So that means that there's a cycle here, right? So we are here and then yeah, it jumped back to here. So yeah, when the hair and turtle we meet at negative four, the length they have run is a plus two B plus C for the hair and a plus B. So this is just like the distance it traveled. Since the fast point is two times faster than the slow pointer, we get a is equal to C. Yeah, so this is just like the math, math logic behind it. If the distance you traveled is twice as fast. In the end, you're going to reach the same location, right? So when another pointer current runs from head to two distance at the same time, previous pointer will run from negative four to two distance. So they begin at the two distance together. So that means that that's the point where the cycle begins. So essentially, is that if you travel twice the distance and you add up all your distances and divided by two, you're going to get like the location where you're going to meet up. Okay, that's what this part is. Okay, when you've reached a cycle. So yeah, that's just like the basic intro problem about slow and fast pointers. And then now let's actually just go over some more problems. So if you want to get into the gist of this, essentially is that one point is going to move twice as fast as the other one. And then sooner or later, if you when when we reach like this same location, in like in this example, yeah, and when we reach the same location, that means that there's going to be a cycle. So yeah, through the link list, we have two pointers, one's going twice as fast, and the other one's going slower. So once you meet the same location, and the same just return true. And that means you're you have a cycle. So let's go over some more problems. All right, guys, we're gonna go over some problems and then we'll be on our way. So a happy number is an algorithm. Basically, it's you have any integer and then you replace the number by adding up the sums of all the digits. And then you repeat this process until the number equals to one, or it loops endlessly in a cycle. So if it ends in one, then it's a happy number, otherwise, it's a false. So here's what the example what it looks like. So we have one squared. So we have n is equal to 19. And then we're going to just going to sum up the square of all the digits. So we have one square plus nine square, right, because the one and nine are the digits here. And then that equals 82. And we just repeat this process eight square plus two square, right, because the digits eight, and the two, right, eight square plus two square. And we get 68. And then we do the same process six square plus eight square, get 100. And then 100, we're just gonna square the digits again. So one square plus zero square plus zero square, we'll give you one. So in the end, this ends up equaling to one. And they said if it equals to one, we're going to return true and false if it's not. Okay, so in the first one, it would have returned true, right? output is true, because the end became one. And the second example is equal to two. And no matter how many times you square the digits and sum them up, you're still going to get false, you're not going to get one. So yeah, this is the question. And let's just go over the solution. So the solution is is actually to use two pointers. So there's a way to do with two pointers. We're just going to go to right here. So actually, let's just go to the discussion tab. So there's there's a way to do this with two pointers. Yeah, use this one. Okay, so here, this isn't Java, but we could do the same thing in C++. But so yeah, here. So we have this function called sqr. And it's just going to sum up the squares of all the digits of the number. So we have sqr is just going to square all the digits of the number. So given the number and it's just going to square all the digits. So basically, it's saying while n is not equal to zero, we're just going to get each of the digit. And we're just going to add up the square of all the digits, right? So the REM squared plus equal to sum, and then we divide n by 10 to get the next digit. So then in the end, this is just going to return the sum of all the digits here, the sum of the square of all the digits here. So that's what this is doing. So how do you use two pointer here is that we're going to have a slow pointer is going to essentially equal to n, and the fast is going to equal to n also. And then while they're both not equal to, well, the fast is not equal to one, and the square of the digits of the fast is not equal to one. We're just going to square all the digits of slow first. And then we're going to do the same thing for fast. But for fast, we're going to do it twice. So we're going to square all the digits, and then we square all the digits again, and sum them all them up. So square all the digits, sum them up, and fast is going to do it twice as fast. So So it's fast, we're going to do square one digit something like, okay. So if the fastest equal to slow, so what that means is that if in the end, we end up getting like the same digit no matter what, so let's say we were at like two, and then we square it all the sum of the digits, we get like four, right. And then a four square would be squirrels I'm just 16, right. So if, and then fast square, yes, that would be fast square and then slow square would be two square or four, four square 16. Also, yeah. So if fast is equal to slow, it's going to return false because no matter what happens, they're not going to equal to one, right, either of them will not equal to one, right. So like if if we repeat this process, right, if we repeat this process of happy number, and we do it like multiple times, and it's both is equal to the same number. So here we have n is equal to two, right. So if we repeated the process twice as much, and then some of all the digits, and we do it again. And if they're the same number, no matter what happens, it means that it can never equal to one, right, because because when fast, if either fast is equal to one or the square of the fat square of the sum of all the digits is equal to one, this while loop would break out of it, right. But if they end up equaling each other, it means no matter what happens, they're, they're not going to equal to one, right. So then if that's the case, we just return false, otherwise return true. So this is like using two pointers in that we're just computing a value function of summing all the digits twice as fast. And we realize that, hey, if we end up actually having the same number in the end, it means it's not possible because it's just going to continue going on and on and on. So yeah. So that's how you do happy number using the two pointer approach. So let's go over the next problem. Okay, the next problem is going to be the middle of the linked list. So given the head, we just have to return the middle of the linked list. So let's say we have list linked list one, two, three, four, five, the middle of it, which is the exact middle here is going to be three. And we want to return three, right? So yeah. And then it just returns like the, it just returns the rest of the nodes, I believe. Yeah. So return three, and then it would be the rest of the nodes here. Now, if it's like an even number, right, even number, and we try to get the even number, if we would try to get the middle of the linked list, I think it just does the right most one, I believe there are two middle notes return the second. Yeah. So one, two, three, four, five, six. So if we just like, cross out both sides here, the middle would be either a three or four. And then they said just return the second node. So that'd be four. And then it returns four, five and six, because that's the middle note and all the values after it, right? They didn't want us to return the middle note and all the values after it. Okay. So how do you do this problem? We could do it with two pointer approach. So let's just go through a simple two point approach. I think it's okay, so this is like a C sharp value. Or not this one. Two pointer should be something like this. Okay, so, okay, so this is how you do it. Okay. Okay, using two pointers. Okay, so all you have to do is just point the slow pointer to equal to the head and the fast pointer is going to go to the head so they both start the same. If the head is null and the next or the heads next null, we just return head because that means that the link list is empty. So what we're going to do is we're just we're just going to go fast. So the fast point is going to go like twice as fast. We have to make sure the next next node is not null, right? So if the fast point is not null, and the next pointer is not null, then that means we're going right. Otherwise, like, we're going to end up like, if we're going twice as fast, and the two times as fast as like, no, then we can't continue, right? So yeah, all you have to do is just set the slow to equal the next and the fast is going to equal twice as fast. In the end, you're going to get to the middle node, and just return slow, slow, which will be the middle node. So yeah, that's all you have to do with the two point approach. It's not a hard this is not a hard problem. But yeah, that's all you have to do for the two point approach. When you're going to go slow, and then the other one's going to do it twice as fast. But yeah, that's it. And let's go over the next problem. Okay, so let's go over another problem called reorder the list. So basically, you're given a link list. And then what we're trying to do is just reorder it so that we have the first node, and then the last node, and the second node, and the second, the last node, the third node, and then the third, the last node. Okay, so the example here, we have 1234. So what we're going to do is we're going to reorder it so that we have the first node one, and then we're going to have the last node four. So last one is going to be four will be here. And we have the second, oh, two, right, the second, second, oh, two, here, and then we're going to have the second or last node three here. So yeah, that's all we do. We have first and last and second to last, then we have second node and then second or last node, and then we go so on and so forth. Okay, that's what we have here. All right. So you have 12345. And then this example, the same thing, one and five. So one and then we go last one five, two, and second, last one four, and three, which would be three. Okay, and yeah. So let's just go over how this how we would do this. So if you want to use the two pointer, so these guys are using stacks, let's just use two pointers. Here is how you would do it using a two pointer. So here, this is in Java, but we could essentially do the same thing. So what they're going to do is here, what they're doing is they're, I don't okay, I don't want to do it this way. There must be like another slow and fast pointer. Let's see. I think this would this is a better one. Yeah, okay. Okay, so here. Yeah, so here, I think this is the two pointer approach that you want to we want to do. Okay, so we have heads and all and the holy heads and necks and all we just return. Okay. So here, what they're doing is they're going to find the middle of the link list with the head, and they have that. Then what were they're going to do is they're going to traverse backwards. They're going to have a reverse link list. So this reverse list is just going to essentially reverse the link list, starting from the current node. So it's just going to reverse it. So this trap is just going to have like the starting from the middle link list to the end, they're going to reverse it. And then what we're going to do is we're going to keep traversing it through and then we're just going to move the current node to the next node and then we're going to move the next to the current node and rewire everything. Okay, so if we wanted to go over an example, it would be something like let's see. Let's see, let's go paint. So let's say we had like 1234, right? So we have 1234. Oh, actually, let's go over 123456. So we have 12345. Okay, so we have 12345. So what they're going to do is they're going to try to find the middle of the node, which is going to be three. Right, so they have code that finds the middle. So that's what this does is, right, finding middle, we just went over that how to do that. Just a few minutes ago. So we find the middle node. And then what we're going to do is we're going to reverse the whole list from mids next. So from here to the end, we're going to reverse it. So this is going to be go to five point to four point to know, okay? Okay, so they'd be five point to four point to null. Okay, now what we're going to do is mids next is going to point to no. So this this next, we're going to break this and we're going to set this to null. Okay. And what we're going to do is we're going to start currents point to head. So our current is going to point to head. So this is our current is going to point to this head node. And traverse is while traverse not equal to null. So now we're going to traverse from here. So traverse from here from five to four after reversing it, right? So yeah, so turn temporary is going to equal to currents next. So currents next, right now points ahead, right? So m is going to equal to currents next to a point to two. Okay. And then what we're going to do is currents next is going to point to trap. So this current of one is going to point to travel, which is five. So this one gets breaking down. It's going to point to five. Okay, because this is trap. So five, this is trap, right? This is a trap. Okay. And then, um, yeah, currents export trap, trap is going to go next. So then trap is going to go to the next pointer. So this trap is going to go to the next pointer. So it goes here. Okay. And then currents next, next is going to equal to temp. So currents next. So temp is this right currents next. So this is so current is this right this is this is current is current. So now currents next, which is five, this five's next is going to point to point to temp. So temp was equal to two, right? So this is temp. So now currents next, a five, five's next, so we're going to break this down. I was actually going to point to two. Okay. And then current is equal to temp. So current, now we're going to move the current down to be equal to temp. So this is going to be, it's going to equal to temp. This is that's current is going to be here now. And then um, yeah, so then Trab. Trab is not equal and all yet. Temp is going to equal the currents next. So currents next will be three. So temp is going to equal to three. So this is going to be temp, 10 points of three. Then um, yeah, 10 points of three currents next is going to go to trap. So this currents next, we break this and we're going to set equal to trap. So traps would be four, right? Um, and then Travis next is going to go to Travis next. So then trap is going to go to equal to null, right? And then currents next next is going to go to temp. So currents, this is currents next of four. This four is next. It's going to point to the temp, which is three. So this is going to be pointing to three. So we're going to go to this breaks. And this is going to point to three. Okay. And then, um, current is going to go to temp. So current is going to equal to three. This current moves down to three. And then yeah, Traverse is going to equal to null. So in the end, we're going to have one pointing to five, five pointing to two, two pointing to four and four pointing to three. So one pointing to five, five pointing to four. No, wait, no, five pointing to two, five pointing to two, one point of five, five pointing to two, two pointing to four. So two points to four. And then four point three. Okay, so then in the end, we actually have like all these nodes. And they're actually, yeah, they'll be all in close. So yeah, we basically just reordered it using two pointers. By one going to like the next next. So yeah, this is kind of not sure if it's really going slow and fast. But I think that's the one that I only could see. Yeah, I don't see any other. Other one is to do like recursion. Yeah, okay, so this is like the exact same thing what we were doing. Slow and fast. Okay, yeah, use here to keep track done less front. Okay. So yeah, there's basically the same thing what we were doing here. Anyway, so yeah, that's the gist of reordering list. And let's go over the next problem. And then I think what's our last problem will be done. All right, guys, we're going to go over one last problem and then we'll be on our way. Circular array loop. Given a game that involves a circular array of nums, each nums of i denotes the number of indices forward and backward, you must move if you are located at index i. So if nums i is positive, remove nums i steps forward. And if nums i is negative, remove nums i step backwards. The array is circular so that if you move forward from the last one, you go goes back to the beginning. If you move backward from the first node, it goes back to the last element. A cycle of the array consists of a sequence of indices of length k where the following movement rules above results in the repeated sequence. So we have sequence of zero sequence of one up to sequence of k minus one up to sequence of zero. Every nums of sequence of j is either all positive or all negative. And k is greater than one return true if there's a cycle, otherwise return false. So here we have two negative one one two two. And this is true because we started index zero move two to the right. Yeah, one. Yeah, and move one to the right we get way so there's a cycle from index zero to zero to two and to three. And then three goes back to one to two. Yeah, index zero. So on and so forth, right? So I started to and I moved two to the right, I get one, move one to the right, get two. If I move to the right, get two again, and I go back to the index zero. So there is a there is a sequence where it goes back to the starting node. Okay. And then every nums of this is all positive, or all negative and the length is greater than one is three. Okay. And yeah, so we just have to make sure all these conditions are met, where we get into a cycle, we go back to the original number. And all the values are either all positive or negative for each number that we go to and the length is greater than one. Okay, so that's essentially the gist of the problem. So let's just go over the solution on how to do this. Yeah. So on fast pointer. Okay. So what we're going to do is we're going to use we're going to loop from beginning to the end. Okay. And slow is going to start at the index of I and fast is going to start at index I. Okay, so here, what they do is they have a next function that just gets the current index, and it would just return it adds the current number and mods it by the length. So it goes back to the front, if it does or it goes back to the back, if it does, okay, so that's what all this stuff does. So if current index plus the current nums index plus zero, we're just going to go loop backwards, and then we'll stop. Otherwise, we're going to do you have to offset less than zero. We subtract by offset. Otherwise, we go back to the front. Okay. And otherwise, if it's greater than zero, then we just return the current index plus the nums at the current index. Okay. So yeah. So this is how it works. We go slow is going to equal to the I and fast is going to equal the I. So if slow is equal to the next number that we hop to, right, that means that we just do nothing, we continue back to the top. Okay. Otherwise, if slow is not equal to the fast, we're just going to move slow to the next hop, and fast is going to go twice as fast. Okay. Fast is going to go twice as fast. Okay. And after this slow should equal the fast, then here they set slow back equal to the original index. And then we're going to do the same thing. slow is going to go to the next number and then fast is going to go next number. And here what they're going to do is they're going to have a previous and a fast. Yeah. And this is just going to count how many times they're doing this, so that we make sure that essentially what we make sure that the size greater than one, right, all the numbers are greater than the size of our sequence that is repeating. That's a cycle is greater than one. So yeah. So that's what they do here. Okay. So that's going to go to zero, flags go through break. Otherwise, previous equal to fast pointer, count plus plus, this is just going to count how many times the size of it is okay. The size greater than one return true. Otherwise, in the end, we return false. So yeah, basically, we're just going to move twice as fast. And if we get to the the same one, we just make sure that all the counts is greater than one and then make sure we go back to the original pointer. There might be a better code that does the same the same thing. Let's see. Oh, this is a better one. He's way better. No, no, I want to like an actual like slow and fast pointer. Let's see if there's a slow and fast pointer. Yeah, yeah, this would be a better one. Yeah. So yeah, it's basically the same thing what we did before. They just have a more conditionals here. So it goes twice as fast and fast goes faster. And then if we reach the same, we break return false if the count is too much. Yeah. All right. So that's basically the gist of these problems. I hope I explained it recently. Ray com subscribe. I'll check you guys later. And yeah, that's the it. That's it for now. Yeah, peace.