1
00:00:00,110 --> 00:00:04,910
Let's now get into the code for the approach that we have discussed in the previous video.

2
00:00:04,940 --> 00:00:05,300
Okay.

3
00:00:05,300 --> 00:00:11,930
So over here we have the function find the winner and n and k are given as input parameters.

4
00:00:11,930 --> 00:00:17,270
Now to start with the solution like let's say it's given that n is equal to three.

5
00:00:17,270 --> 00:00:23,900
So if this is the case then we want to create an array which will have the elements one, two and three

6
00:00:23,900 --> 00:00:24,230
okay.

7
00:00:24,230 --> 00:00:30,350
So similarly if n was four we would want to create an array which has the elements one, two three and

8
00:00:30,350 --> 00:00:32,480
four which represents the people okay.

9
00:00:32,480 --> 00:00:36,680
So n represents how many people are there when this game is played.

10
00:00:36,680 --> 00:00:50,090
So for this let's say let R is equal to array dot from length n okay comma I I plus one.

11
00:00:50,990 --> 00:00:51,680
All right.

12
00:00:51,680 --> 00:00:57,290
So again by now based on the input parameter n we have created an array.

13
00:00:57,290 --> 00:01:00,830
So again it will have the values from one up to n.

14
00:01:00,830 --> 00:01:04,910
Now again as we have discussed this is going to be a recursive function.

15
00:01:04,910 --> 00:01:07,910
So again let's write the skeleton of the solution first.

16
00:01:07,910 --> 00:01:10,700
And then we'll go ahead and write the details.

17
00:01:10,700 --> 00:01:14,210
So to start with we are going to have a helper function.

18
00:01:14,210 --> 00:01:16,280
Let's just call it helper itself.

19
00:01:16,280 --> 00:01:24,530
And to this function we will be passing the array and also the start index which is the index from where

20
00:01:24,530 --> 00:01:27,620
we have to start to count when this game is played.

21
00:01:27,620 --> 00:01:30,200
So let's write the details of the function later.

22
00:01:30,200 --> 00:01:33,050
And we would have to call the function once.

23
00:01:34,700 --> 00:01:39,620
And when we call it for the first time we will be passing the array, which is what we have created

24
00:01:39,620 --> 00:01:40,250
over here.

25
00:01:40,250 --> 00:01:47,360
And the start index is going to be equal to zero because we'll start counting at the index zero okay.

26
00:01:47,360 --> 00:01:50,810
And again, whatever we get back from this we are just going to return.

27
00:01:50,810 --> 00:01:53,270
So that's how we are going to write the helper function.

28
00:01:53,270 --> 00:01:54,020
All right.

29
00:01:54,020 --> 00:02:00,410
So again this is fine because notice that the elements in the array don't start with the value zero.

30
00:02:00,440 --> 00:02:01,880
They start with the value one.

31
00:02:01,880 --> 00:02:08,060
So it's fine that we can just directly return whatever we get back from this, because we will write

32
00:02:08,060 --> 00:02:13,520
it in a way that it's going to be a particular element from this array, and that would indicate the

33
00:02:13,520 --> 00:02:16,310
person who emerged as the winner of the game.

34
00:02:16,640 --> 00:02:17,090
All right.

35
00:02:17,090 --> 00:02:19,340
So this is the skeleton of the approach.

36
00:02:19,340 --> 00:02:21,500
Now let's go ahead and write the details.

37
00:02:21,500 --> 00:02:27,320
So in the helper function I'm going to write the base case as well as the recursive case.

38
00:02:29,350 --> 00:02:30,010
All right.

39
00:02:30,010 --> 00:02:36,790
Now the base case is going to be the case when there's just one element left in the array.

40
00:02:36,820 --> 00:02:37,240
Okay.

41
00:02:37,240 --> 00:02:42,790
So we can say if our dot length is equal to one.

42
00:02:42,910 --> 00:02:48,790
So if there is just one person in the array then that particular person would be the winner.

43
00:02:48,790 --> 00:02:49,150
Right.

44
00:02:49,150 --> 00:02:52,900
And again in the array that element is at index zero.

45
00:02:52,900 --> 00:02:59,170
So all that we need to do in this case is that we need to return our at index zero.

46
00:03:00,220 --> 00:03:00,610
All right.

47
00:03:00,610 --> 00:03:02,170
So that's the base case.

48
00:03:02,170 --> 00:03:05,110
Now let's go ahead and write the recursive case.

49
00:03:05,140 --> 00:03:11,530
Now for this first we'll have to identify the index which has to be removed okay.

50
00:03:11,530 --> 00:03:13,570
So let's just call it index to remove.

51
00:03:13,660 --> 00:03:19,450
And this would be equal to start index which is what we're getting over here.

52
00:03:19,540 --> 00:03:22,570
Plus k minus one.

53
00:03:22,570 --> 00:03:26,800
And over here we'll have to do modulo p length of the array length.

54
00:03:26,800 --> 00:03:29,200
And that particular instance okay.

55
00:03:29,500 --> 00:03:31,990
So that's modulo our dot length.

56
00:03:32,560 --> 00:03:38,410
And again we have discussed the details or the logic for this in detail in the previous video.

57
00:03:38,440 --> 00:03:42,070
Now over here we have got the index which has to be removed.

58
00:03:42,070 --> 00:03:47,770
Now we need to remove the element at that particular index in the array.

59
00:03:47,770 --> 00:03:49,960
So I can say r dot splice.

60
00:03:53,040 --> 00:03:56,340
Index to remove comma one okay.

61
00:03:56,340 --> 00:03:58,740
So just one element has to be removed.

62
00:03:58,740 --> 00:04:03,600
And the element at this particular index is the element which we want to remove.

63
00:04:03,600 --> 00:04:08,700
And after that we're going to recursively call the helper function again.

64
00:04:08,700 --> 00:04:10,290
And we will pass R to it.

65
00:04:10,290 --> 00:04:14,640
And we will pass index to remove as the start index okay.

66
00:04:14,640 --> 00:04:17,340
And again we have discussed this in detail why this works.

67
00:04:17,340 --> 00:04:18,330
So that's it.

68
00:04:18,330 --> 00:04:20,100
So this should solve the question.

69
00:04:20,100 --> 00:04:24,300
Now let's go ahead and run this code and see if it's passing all the test cases.

70
00:04:27,530 --> 00:04:30,800
And you can see that it's passing all the test cases.

71
00:04:30,830 --> 00:04:31,310
All right.

72
00:04:31,340 --> 00:04:33,170
Now do make use of the user logs.

73
00:04:33,200 --> 00:04:37,490
They will help you debug your code in case you're stuck at some point.
