1
00:00:00,610 --> 00:00:01,570
Welcome back.

2
00:00:01,570 --> 00:00:05,020
Let's now code the solution that we discussed in the previous video.

3
00:00:05,050 --> 00:00:10,240
Now first we will need a function which will help us to reverse part of the array.

4
00:00:10,240 --> 00:00:10,510
Right.

5
00:00:10,510 --> 00:00:13,210
So we are going to reverse two parts of the array separately.

6
00:00:13,210 --> 00:00:15,280
So let's just call this function reverse.

7
00:00:15,280 --> 00:00:18,040
And this function is going to take in an array.

8
00:00:18,040 --> 00:00:23,380
And it is going to take in the indices of the start and the end between which we want to reverse the

9
00:00:23,380 --> 00:00:23,860
values.

10
00:00:23,860 --> 00:00:26,170
So this is going to be our helper function.

11
00:00:26,170 --> 00:00:28,360
And then we have our main function.

12
00:00:28,360 --> 00:00:30,010
Let's just call it rotate.

13
00:00:30,550 --> 00:00:34,090
Now this function is going to take in the array of integers.

14
00:00:34,090 --> 00:00:39,280
And then it's going to take in a value k which indicates how many times we want to rotate it towards

15
00:00:39,280 --> 00:00:39,910
the right.

16
00:00:39,910 --> 00:00:40,420
All right.

17
00:00:40,450 --> 00:00:44,020
Now let's go ahead and first write the rotate function over here.

18
00:00:44,020 --> 00:00:48,940
Now first we are going to say k is equal to k modulo numb's dot length.

19
00:00:48,940 --> 00:00:49,330
Right.

20
00:00:49,330 --> 00:00:50,860
So why are we doing this.

21
00:00:50,860 --> 00:00:53,410
Let's say k is equal to 102.

22
00:00:53,410 --> 00:00:55,720
Then we don't want to do 102 rotations.

23
00:00:55,720 --> 00:00:56,110
Right.

24
00:00:56,110 --> 00:01:01,630
Because every time numb's dot length rotations are done, for example, let's say length is equal to

25
00:01:01,630 --> 00:01:02,140
five.

26
00:01:02,140 --> 00:01:06,400
So every time five rotations are done we are going to get back to the initial position.

27
00:01:06,400 --> 00:01:06,700
Right.

28
00:01:06,700 --> 00:01:09,400
So 102 is equal to five into 20 plus two.

29
00:01:09,400 --> 00:01:12,100
So we're going to take all the multiples of five out.

30
00:01:12,100 --> 00:01:13,690
And we are just left with two right.

31
00:01:13,690 --> 00:01:16,870
So we actually just need to do two rotations in this case.

32
00:01:17,650 --> 00:01:21,820
So that is what we're doing over here k is equal to k modulo numb's dot length.

33
00:01:21,820 --> 00:01:26,020
Over here if k is 102 and length is five, this will just give us the remainder.

34
00:01:26,020 --> 00:01:28,300
When we divide 102 by five.

35
00:01:28,300 --> 00:01:30,040
So k will become equal to two.

36
00:01:30,070 --> 00:01:31,990
If k is 102 and length is five.

37
00:01:31,990 --> 00:01:33,490
So that is what we're doing over here.

38
00:01:33,490 --> 00:01:38,290
Now we are just going to reverse the array which is given to us.

39
00:01:38,290 --> 00:01:40,810
So we can either do numb's dot reverse.

40
00:01:40,810 --> 00:01:43,060
So which is an inbuilt function to reverse it.

41
00:01:43,060 --> 00:01:46,780
So we can do this or because we are going to anyway write the helper function.

42
00:01:46,780 --> 00:01:49,450
Let me call the reverse function which we are writing.

43
00:01:49,450 --> 00:01:50,740
We will pass numb's.

44
00:01:50,740 --> 00:01:53,530
Now at this point we are reversing the whole array right?

45
00:01:53,530 --> 00:01:58,180
So the start will be zero and the end will be numb's dot length minus one.

46
00:01:59,840 --> 00:02:00,320
All right.

47
00:02:00,320 --> 00:02:01,190
So.

48
00:02:02,000 --> 00:02:04,820
Now we have reversed the array which is given to us.

49
00:02:04,820 --> 00:02:09,590
Now remember, we have to reverse the two parts of the array for the first part.

50
00:02:09,620 --> 00:02:15,080
The start will be equal to zero and the end will be equal to k minus one, as we have discussed in the

51
00:02:15,080 --> 00:02:15,350
video.

52
00:02:15,350 --> 00:02:15,620
Right.

53
00:02:15,620 --> 00:02:18,170
So let's go ahead and reverse that part of the array.

54
00:02:18,170 --> 00:02:20,930
So we pass in Numb's again which is now reversed.

55
00:02:20,930 --> 00:02:24,140
And start is zero and end is k minus one.

56
00:02:24,140 --> 00:02:30,770
At this point now we are going to reverse the remaining part of the array where start is equal to k.

57
00:02:32,110 --> 00:02:35,110
And end is the, uh, length minus one.

58
00:02:35,110 --> 00:02:35,290
Right?

59
00:02:35,290 --> 00:02:37,840
So end is the length of the given array minus one.

60
00:02:37,840 --> 00:02:38,890
So let's do that.

61
00:02:38,890 --> 00:02:41,380
So again we call the helper function reverse.

62
00:02:41,380 --> 00:02:43,420
And we pass in the array numb's.

63
00:02:43,420 --> 00:02:48,850
And at this point the start is k and the end is numb's dot length minus one.

64
00:02:50,230 --> 00:02:50,680
All right.

65
00:02:50,680 --> 00:02:51,700
And then we are done.

66
00:02:51,700 --> 00:02:53,590
Actually, we can just return the array.

67
00:02:53,620 --> 00:02:58,990
Now all we have to do is we have to write the reverse function, which is a simple function where we

68
00:02:58,990 --> 00:03:03,820
just reverse the values between start and end for the given array.

69
00:03:03,850 --> 00:03:05,830
Now we're going to have a while loop over here.

70
00:03:05,830 --> 00:03:12,010
So while start less than end we are just going to swap the values at start and end.

71
00:03:12,010 --> 00:03:14,950
And then we're going to increment start and decrement end.

72
00:03:14,980 --> 00:03:15,970
That's it over here.

73
00:03:16,000 --> 00:03:18,040
Now we can use a temporary variable.

74
00:03:18,040 --> 00:03:23,230
Or we can use this syntax where we're going to pass the two values which is numb's.

75
00:03:24,050 --> 00:03:27,170
At start comma Numb's.

76
00:03:28,380 --> 00:03:31,260
At end and then we want to swap them right.

77
00:03:31,260 --> 00:03:33,420
So over here also we do the swap value.

78
00:03:33,420 --> 00:03:34,290
So we say.

79
00:03:35,220 --> 00:03:37,650
Numb's at end.

80
00:03:38,880 --> 00:03:41,790
Comma numb's at start.

81
00:03:42,390 --> 00:03:45,990
So we can use this ES6 syntax or we can just use a temporary variable.

82
00:03:45,990 --> 00:03:47,130
Also it's the same thing.

83
00:03:47,130 --> 00:03:49,260
So we are swapping the values over here.

84
00:03:49,260 --> 00:03:51,180
And then we are going to increment start.

85
00:03:51,180 --> 00:03:55,350
So start plus plus and end minus minus.

86
00:03:55,920 --> 00:03:56,670
And that is it.

87
00:03:56,670 --> 00:03:58,590
So we are done with our solution.

88
00:03:58,590 --> 00:04:01,380
Now let's go ahead and test our solution.

89
00:04:01,380 --> 00:04:04,140
Now I am going to call the function.

90
00:04:04,140 --> 00:04:05,580
So let's say rotate.

91
00:04:06,180 --> 00:04:09,420
And let's pass in an array one.

92
00:04:10,660 --> 00:04:14,530
Two, 3456 and let's do six rotations.

93
00:04:14,560 --> 00:04:17,590
Now if you do this we should get back the initial array right.

94
00:04:17,590 --> 00:04:18,820
So let's test it.

95
00:04:19,750 --> 00:04:20,680
When we are doing this?

96
00:04:20,680 --> 00:04:22,990
Yes, we are getting the initial array back.

97
00:04:23,020 --> 00:04:26,410
Now what if we do just two rotations?

98
00:04:26,440 --> 00:04:27,550
Is it working?

99
00:04:27,580 --> 00:04:30,700
So over here we have five and six and then 1234.

100
00:04:30,700 --> 00:04:32,080
Yes that's also working right.

101
00:04:32,080 --> 00:04:34,840
And what if an empty array is passed to it.

102
00:04:36,300 --> 00:04:37,830
We just returning an empty array?

103
00:04:38,160 --> 00:04:41,400
And let's say we have one, two, three.

104
00:04:41,430 --> 00:04:42,780
We are doing two rotations.

105
00:04:42,780 --> 00:04:44,010
We get two, three, one.

106
00:04:44,010 --> 00:04:44,250
Yes.

107
00:04:44,250 --> 00:04:45,840
So our function is working.

108
00:04:45,840 --> 00:04:52,560
And as we discussed this is going to operate in O of one space because we are not using any auxiliary

109
00:04:52,560 --> 00:04:53,160
space.

110
00:04:53,160 --> 00:04:55,680
And the time complexity is still O of N.

111
00:04:55,680 --> 00:04:56,040
Right.

112
00:04:56,040 --> 00:05:00,330
So over here we have O of n operations to reverse the array.

113
00:05:00,330 --> 00:05:02,790
And then over here also it's an O of n operation.

114
00:05:02,790 --> 00:05:06,300
And over here these two together right will be an O of n operation.

115
00:05:06,660 --> 00:05:09,120
So the time complexity converges to O of n.

116
00:05:09,120 --> 00:05:14,010
And the space complexity of this solution is equal to O of one, because we are not using any extra

117
00:05:14,010 --> 00:05:14,610
space.
