Let's now get into the code for the iterative approach that we have discussed in the previous video.
Okay.
So initially we will say let's survivor is equal to zero.
And finally we will do something over here to find the exact index of the survivor.
And then we will go ahead and return survivor plus one okay.
So that's the skeleton of the solution.
Now over here what we are going to do is we are going to iterate from two up to n, where n is the size
of the problem which is given to us.
Okay.
And again we have discussed why we are going to start at two because if n is equal to one, then we
just need to return zero plus one which is equal to one.
So we don't even need to get into this for loop.
So that's why over here we are going to start to iterate from two.
So I can say for let I is equal to two and we are going to go up to n.
So I less than equal to n and I plus plus okay.
Now for each value of I we are going to find the survivor using the previous value of the survivor.
So I can say survivor is equal to survivor plus k.
And then we will have to do modulo the size of the problem at that particular instance, which is given
by the value of I.
So over here I will do modulo I.
Okay.
So again that's it.
So by the time we are out of this for loop we will have found the survivor.
And then we will just have to add one to it.
Because the survivor that we are getting is going to be zero indexed okay.
So that's it.
Now this is the solution.
Now let's go ahead and run this code and see whether it's passing all the test cases.
And you can see that it's passing all the test cases.