Opening the reading…
Opening the reading…
RECURSION & BACKTRACKING › RECURSION PROBLEMS
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →The largest disk cannot move until every one of the n - 1 smaller disks has left the source rod. Those smaller disks must be stacked safely on the auxiliary rod, which is exactly the same problem with one fewer disk. After the largest disk moves once to the destination, the n - 1 smaller disks must move again, this time from the auxiliary rod to the destination.
If M(n) is the minimum number of moves for n disks, the required process has three unavoidable parts: M(n - 1) moves before the largest disk, one move for that largest disk, and M(n - 1) moves afterward. Therefore M(n) = 2M(n - 1) + 1, with M(0) = 0. Expanding the recurrence gives M(n) = 2^n - 1, so counting moves never requires constructing the sequence.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) | The loop doubles one integer exactly n times, and each iteration performs constant work. No disk arrangement or move sequence is generated, so no operation is repeated over the rods. |
| Space | O(1) extra | Only the integer holding the current power and the final count are stored; the required output is a single integer, so there is no output storage to exclude. The bound stays O(1) for every input shape, unlike the recursive formulation whose call stack reaches O(n) in its deepest shape. |
#include <vector>
using namespace std;
class Solution {
public:
int towerOfHanoi(int n) {
int moves = 1;
for (int i = 0; i < n; ++i) {
moves *= 2;
}
return moves - 1;
}
};The initial value moves = 1 represents 2^0, not the answer for one disk. Each loop iteration advances the exponent by one, so after n iterations moves is 2^n. Subtracting one at the return converts that power into the required minimum count and also handles n = 0 naturally.
A recursive implementation mirrors the unavoidable process directly: solve n - 1, count the largest disk's move, then solve n - 1 again. It is a reasonable way to derive and explain the formula, but it is not an optimisation for this task. It performs O(n) calls and uses O(n) call-stack space, whereas the iterative closed form keeps only the current count.
#include <vector>
using namespace std;
class Solution {
public:
int towerOfHanoi(int n) {
return countMoves(n);
}
private:
int countMoves(int disks) {
if (disks == 0) {
return 0;
}
return 2 * countMoves(disks - 1) + 1;
}
};This recursive version is a different arrangement rather than a better algorithm. Its base case states the empty-stack fact, and its return line states the recurrence exactly. With n limited to 20, it remains safe here, but the iterative form is preferable when the judge asks only for the count because it removes the call stack and makes the closed form explicit.