Let's now get into the code for the approach that we have discussed in the previous video.
Okay.
So over here we have the function find the winner and n and k are given as input parameters.
Now to start with the solution like let's say it's given that n is equal to three.
So if this is the case then we want to create an array which will have the elements one, two and three
okay.
So similarly if n was four we would want to create an array which has the elements one, two three and
four which represents the people okay.
So n represents how many people are there when this game is played.
So for this let's say let R is equal to array dot from length n okay comma I I plus one.
All right.
So again by now based on the input parameter n we have created an array.
So again it will have the values from one up to n.
Now again as we have discussed this is going to be a recursive function.
So again let's write the skeleton of the solution first.
And then we'll go ahead and write the details.
So to start with we are going to have a helper function.
Let's just call it helper itself.
And to this function we will be passing the array and also the start index which is the index from where
we have to start to count when this game is played.
So let's write the details of the function later.
And we would have to call the function once.
And when we call it for the first time we will be passing the array, which is what we have created
over here.
And the start index is going to be equal to zero because we'll start counting at the index zero okay.
And again, whatever we get back from this we are just going to return.
So that's how we are going to write the helper function.
All right.
So again this is fine because notice that the elements in the array don't start with the value zero.
They start with the value one.
So it's fine that we can just directly return whatever we get back from this, because we will write
it in a way that it's going to be a particular element from this array, and that would indicate the
person who emerged as the winner of the game.
All right.
So this is the skeleton of the approach.
Now let's go ahead and write the details.
So in the helper function I'm going to write the base case as well as the recursive case.
All right.
Now the base case is going to be the case when there's just one element left in the array.
Okay.
So we can say if our dot length is equal to one.
So if there is just one person in the array then that particular person would be the winner.
Right.
And again in the array that element is at index zero.
So all that we need to do in this case is that we need to return our at index zero.
All right.
So that's the base case.
Now let's go ahead and write the recursive case.
Now for this first we'll have to identify the index which has to be removed okay.
So let's just call it index to remove.
And this would be equal to start index which is what we're getting over here.
Plus k minus one.
And over here we'll have to do modulo p length of the array length.
And that particular instance okay.
So that's modulo our dot length.
And again we have discussed the details or the logic for this in detail in the previous video.
Now over here we have got the index which has to be removed.
Now we need to remove the element at that particular index in the array.
So I can say r dot splice.
Index to remove comma one okay.
So just one element has to be removed.
And the element at this particular index is the element which we want to remove.
And after that we're going to recursively call the helper function again.
And we will pass R to it.
And we will pass index to remove as the start index okay.
And again we have discussed this in detail why this works.
So that's it.
So this should solve the question.
Now let's go ahead and run this code and see if it's passing all the test cases.
And you can see that it's passing all the test cases.
All right.
Now do make use of the user logs.
They will help you debug your code in case you're stuck at some point.