Let's now get into the code for solving the Tower of Hanoi problem.
Okay, so as per the question, there are two things that we need to achieve.
So for every movement of a disc we will have to print a statement.
And we are given a sample console.log statement over here.
And we also need to finally return the number of moves that were needed.
Okay.
So we have to move n disks from the from road to the two road using an auxiliary road.
So that's the question at hand.
Now before we get into the details of the solution, let's just first write down the skeleton of the
approach that we are going to take.
So over here it's clearly mentioned that we need to return the number of moves.
So to track that we will need a count variable.
So let me say let count is equal to zero.
And finally we will be returning count okay.
So let's write that also.
All right.
And as we've discussed in the previous video, this is going to be a recursive solution.
And we will need a helper function.
So let me just call the function helper itself.
And to this function we will be passing the parameters in from two and ox okay.
And we'll have to call the function one time over here.
So when we call the function for the first time we call it with the parameters which are given to us
over here itself, which is n from.
Two and ox.
Okay, so this means that move n disks from the from road to the to road using the auxiliary road.
All right.
So this is the skeleton of the approach that we have.
Now let's go ahead and write the details of this helper function over here.
Now in the helper function, we will be writing both the base case as well as the recursive case.
Okay.
Now, what would be the base case?
The base case would be the scenario when n is equal to one okay.
So if n is equal to one then we have reached the base case.
Now again if there is just one disk that we need to move from the from rod to the two rod, then this
is just a trivial problem.
And we can just directly move the disk from the from rod to the two rod.
Okay.
And remember, whenever we move a disk we will have to print a statement.
So let's copy this over here.
So we are given this sample console log statement.
So let's copy it and paste it over here.
And you can leave this as it is like it's written one over here.
And we know that n is equal to one.
But again let me just write n over here.
And we have to move this particular disk from the from rod okay.
And we have the from rod over here.
So that's from to the two rod.
So let's write two over here okay.
And again remember we also have to increment the count variable.
So count plus equal to one.
And that's it.
And we can just return from here.
So this would be the base case.
Now let's go ahead and write the recursive case.
Now as part of the recursive case, as we have discussed in detail in the previous video, there are
three things that we need to do.
And again, the recursive case is the scenario where we have to move more than one disk from the from
root to the two road.
Okay.
And again we have discussed the criteria that has to be preserved.
That is a bigger disk has to be above a smaller disk.
Okay.
So there are three things that we have to do over here.
First we have to move n minus one disks from the from road to the auxiliary road okay.
After that we can move the last disk which is the nth disk from the from road to the two road.
Okay.
And the third step is we will have to move these n minus one disks which are now at the auxiliary road.
So we will have to shift them from the auxiliary road to the two road.
So these are the three steps that we would have to do.
Now these two steps over here will be recursive calls to this helper function itself.
And over here because we are just moving one disk from a particular road to another road, this would
just be a print statement.
So let's go ahead and write the details over here.
Now to move n minus one disks again, as we have discussed will be recursively calling the helper function.
And now the input parameters would be n minus one.
And then from because we are moving the disks from the from road.
But over here we will have the auxiliary road because we are moving the disks to the auxiliary road.
And in this case the initial road which was the two road would serve as the auxiliary road.
Okay.
Now let's move to the next step over here, which is to move the nth disk from the from road to the
two road.
And because as we have discussed, this is just the movement of one disk from a particular road to another
road.
This would just be a console log statement.
So let me copy this over here and let's paste this over here.
So we are going to move the disk n from the from road to the two road.
So this looks good.
And whenever we are moving a disk we will also have to increment the count variable okay.
So let me write over here count plus equal to one.
And the last step is to move the n minus one disks from the auxiliary road to the to road.
And again this would be achieved using a recursive call to the helper function where the input parameters
are n minus one ox.
Because that's where the disks are at this point.
So auxiliary road.
The initial auxiliary road is the from road over here.
And then we are moving them to the two road.
And the initial from road would serve as the auxiliary road in this case.
Okay.
And that's it.
So this should help solve the problem.
Now let's go ahead and run this code and see if it's passing all the test cases.
And you can see that it's passing all the test cases.
Now again, just a quick reminder.
Do make use of the user logs.
Over here you can see the input.
So for example if n is equal to three and from prod is one and the two rod is three and the auxiliary
rod is two, then these are the movements that are required and the expected output is seven.
Movements of disks are involved and the result is also seven.
You will get these statements over here in both scenarios like for example, even if your test case
fails or even if it passes, you will get these statements over here.
So do make use of these user logs.
They will help you debug your code in case you're stuck at some point.
And again, if you want to add some user logs, you can always do that by just having more console log
statements.
And then you just need to click run code.