Mark As Completed Discussion

An Awesome Recursive Solution

The best way to figure out a solution is to think logically and recursively. Break down the problem into a smaller version of itself.

If we can somehow figure out how to move N-1 disks from source shelf to intermediate shelf, using the destination as an intermediate location, then we can move the biggest disk from source to destination. Next, we can move the N-1 disks from intermediate shelf to the target, using source shelf as intermediate location. The diagram below will help in visualizing this.

An Awesome Recursive Solution

The pseudo-code is therefore very simple.

CPP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment