Welcome back.
Let's now code the solution that we discussed in the previous video.
Now first we will need a function which will help us to reverse part of the array.
Right.
So we are going to reverse two parts of the array separately.
So let's just call this function reverse.
And this function is going to take in an array.
And it is going to take in the indices of the start and the end between which we want to reverse the
values.
So this is going to be our helper function.
And then we have our main function.
Let's just call it rotate.
Now this function is going to take in the array of integers.
And then it's going to take in a value k which indicates how many times we want to rotate it towards
the right.
All right.
Now let's go ahead and first write the rotate function over here.
Now first we are going to say k is equal to k modulo numb's dot length.
Right.
So why are we doing this.
Let's say k is equal to 102.
Then we don't want to do 102 rotations.
Right.
Because every time numb's dot length rotations are done, for example, let's say length is equal to
five.
So every time five rotations are done we are going to get back to the initial position.
Right.
So 102 is equal to five into 20 plus two.
So we're going to take all the multiples of five out.
And we are just left with two right.
So we actually just need to do two rotations in this case.
So that is what we're doing over here k is equal to k modulo numb's dot length.
Over here if k is 102 and length is five, this will just give us the remainder.
When we divide 102 by five.
So k will become equal to two.
If k is 102 and length is five.
So that is what we're doing over here.
Now we are just going to reverse the array which is given to us.
So we can either do numb's dot reverse.
So which is an inbuilt function to reverse it.
So we can do this or because we are going to anyway write the helper function.
Let me call the reverse function which we are writing.
We will pass numb's.
Now at this point we are reversing the whole array right?
So the start will be zero and the end will be numb's dot length minus one.
All right.
So.
Now we have reversed the array which is given to us.
Now remember, we have to reverse the two parts of the array for the first part.
The start will be equal to zero and the end will be equal to k minus one, as we have discussed in the
video.
Right.
So let's go ahead and reverse that part of the array.
So we pass in Numb's again which is now reversed.
And start is zero and end is k minus one.
At this point now we are going to reverse the remaining part of the array where start is equal to k.
And end is the, uh, length minus one.
Right?
So end is the length of the given array minus one.
So let's do that.
So again we call the helper function reverse.
And we pass in the array numb's.
And at this point the start is k and the end is numb's dot length minus one.
All right.
And then we are done.
Actually, we can just return the array.
Now all we have to do is we have to write the reverse function, which is a simple function where we
just reverse the values between start and end for the given array.
Now we're going to have a while loop over here.
So while start less than end we are just going to swap the values at start and end.
And then we're going to increment start and decrement end.
That's it over here.
Now we can use a temporary variable.
Or we can use this syntax where we're going to pass the two values which is numb's.
At start comma Numb's.
At end and then we want to swap them right.
So over here also we do the swap value.
So we say.
Numb's at end.
Comma numb's at start.
So we can use this ES6 syntax or we can just use a temporary variable.
Also it's the same thing.
So we are swapping the values over here.
And then we are going to increment start.
So start plus plus and end minus minus.
And that is it.
So we are done with our solution.
Now let's go ahead and test our solution.
Now I am going to call the function.
So let's say rotate.
And let's pass in an array one.
Two, 3456 and let's do six rotations.
Now if you do this we should get back the initial array right.
So let's test it.
When we are doing this?
Yes, we are getting the initial array back.
Now what if we do just two rotations?
Is it working?
So over here we have five and six and then 1234.
Yes that's also working right.
And what if an empty array is passed to it.
We just returning an empty array?
And let's say we have one, two, three.
We are doing two rotations.
We get two, three, one.
Yes.
So our function is working.
And as we discussed this is going to operate in O of one space because we are not using any auxiliary
space.
And the time complexity is still O of N.
Right.
So over here we have O of n operations to reverse the array.
And then over here also it's an O of n operation.
And over here these two together right will be an O of n operation.
So the time complexity converges to O of n.
And the space complexity of this solution is equal to O of one, because we are not using any extra
space.