1
00:00:00,080 --> 00:00:05,510
Let's now get into the code for the iterative approach that we have discussed in the previous video.

2
00:00:05,510 --> 00:00:05,840
Okay.

3
00:00:05,840 --> 00:00:11,600
So initially we will say let's survivor is equal to zero.

4
00:00:11,720 --> 00:00:17,660
And finally we will do something over here to find the exact index of the survivor.

5
00:00:17,660 --> 00:00:23,180
And then we will go ahead and return survivor plus one okay.

6
00:00:23,180 --> 00:00:25,460
So that's the skeleton of the solution.

7
00:00:25,460 --> 00:00:32,780
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

8
00:00:32,780 --> 00:00:34,460
of the problem which is given to us.

9
00:00:34,460 --> 00:00:34,940
Okay.

10
00:00:34,940 --> 00:00:41,090
And again we have discussed why we are going to start at two because if n is equal to one, then we

11
00:00:41,090 --> 00:00:44,120
just need to return zero plus one which is equal to one.

12
00:00:44,120 --> 00:00:46,760
So we don't even need to get into this for loop.

13
00:00:46,760 --> 00:00:50,780
So that's why over here we are going to start to iterate from two.

14
00:00:50,810 --> 00:00:56,720
So I can say for let I is equal to two and we are going to go up to n.

15
00:00:56,720 --> 00:01:02,540
So I less than equal to n and I plus plus okay.

16
00:01:02,540 --> 00:01:09,530
Now for each value of I we are going to find the survivor using the previous value of the survivor.

17
00:01:09,530 --> 00:01:15,380
So I can say survivor is equal to survivor plus k.

18
00:01:15,380 --> 00:01:22,010
And then we will have to do modulo the size of the problem at that particular instance, which is given

19
00:01:22,010 --> 00:01:23,510
by the value of I.

20
00:01:23,540 --> 00:01:25,880
So over here I will do modulo I.

21
00:01:26,270 --> 00:01:26,720
Okay.

22
00:01:26,720 --> 00:01:28,010
So again that's it.

23
00:01:28,010 --> 00:01:32,690
So by the time we are out of this for loop we will have found the survivor.

24
00:01:32,690 --> 00:01:35,630
And then we will just have to add one to it.

25
00:01:35,630 --> 00:01:40,700
Because the survivor that we are getting is going to be zero indexed okay.

26
00:01:40,700 --> 00:01:41,390
So that's it.

27
00:01:41,390 --> 00:01:42,920
Now this is the solution.

28
00:01:42,920 --> 00:01:47,840
Now let's go ahead and run this code and see whether it's passing all the test cases.

29
00:01:51,730 --> 00:01:54,880
And you can see that it's passing all the test cases.
