Search code examples
algorithmdata-structuresdivide-and-conquer

A question regarding the tower of hanoi recursive algorithm time complexity


I am doing a coding exercise today. After finishing the examination, I checked the results and I faced a problem whose problem statement is shown as follows:

Given 4 disks in the tower of Hanoi problem, the recursive algorithm calls the same function at most ___ times.
A. 10
B. 16
C. 22
D. 31

The only thing I knew is that I selected B. 16 and I was wrong.
I searched on the internet upon discovering that it should be 2n - 1 times, or 15 times.
However, it was not in the options.
Which option is correct?
Any advice will be appreciated.
Thank you.


Solution

  • The 4-disk puzzle takes 15 moves. The number of recursive calls, though, depends on how it's implemented.

    If your recursive base case is 1 disk => 1 move, then it's 15. If your recursive base case is 0 disks => 0 moves, then it's 31.