Tower of Hanoi Problem in DSA C - Time & Space Complexity
We want to understand how the time needed to solve the Tower of Hanoi grows as we add more disks.
How does the number of moves change when the number of disks increases?
Analyze the time complexity of the following code snippet.
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
This code moves n disks from one rod to another using recursion and an auxiliary rod.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls that move disks one by one.
- How many times: Each call splits into two smaller calls with n-1 disks, plus one move.
Each time we add a disk, the number of moves roughly doubles and adds one more move.
| Input Size (n) | Approx. Operations (Moves) |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 7 |
| 4 | 15 |
| 5 | 31 |
Pattern observation: The moves grow exponentially, doubling each time plus one extra move.
Time Complexity: O(2^n)
This means the time needed doubles with each additional disk, growing very fast as n increases.
[X] Wrong: "The time grows linearly with the number of disks because we move each disk once."
[OK] Correct: Actually, disks are moved multiple times in a recursive pattern, causing exponential growth, not just once each.
Understanding this problem helps you think about recursion and exponential growth, which are common in many coding challenges.
"What if we changed the problem to move disks iteratively without recursion? How would the time complexity change?"
Practice
Solution
Step 1: Understand the formula for minimum moves
The minimum moves required for n disks is 2^n - 1.Step 2: Calculate for 3 disks
For 3 disks, minimum moves = 2^3 - 1 = 8 - 1 = 7.Final Answer:
7 -> Option DQuick Check:
2^3 - 1 = 7 [OK]
- Using n instead of 2^n - 1
- Calculating 2*n - 1 instead of 2^n - 1
- Confusing number of disks with moves
Solution
Step 1: Identify parameters needed for Tower of Hanoi
We need number of disks and names of three rods: source, destination, auxiliary.Step 2: Check function signature matching these parameters
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod); has int n and three char rods, matching the problem requirements.Final Answer:
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod); -> Option AQuick Check:
Function needs 3 rods and disk count [OK]
- Missing auxiliary rod parameter
- Using int instead of char for rods
- Returning int instead of void
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
int main() {
towerOfHanoi(2, 'A', 'C', 'B');
return 0;
}Solution
Step 1: Trace recursive calls for n=2
First move disk 1 from A to B, then disk 2 from A to C, then disk 1 from B to C.Step 2: Match output with options
Move disk 1 from rod A to rod B Move disk 2 from rod A to rod C Move disk 1 from rod B to rod C matches the exact sequence of moves printed by the code.Final Answer:
Move disk 1 from rod A to rod B Move disk 2 from rod A to rod C Move disk 1 from rod B to rod C -> Option CQuick Check:
Recursive moves for 2 disks = Move disk 1 from rod A to rod B Move disk 2 from rod A to rod C Move disk 1 from rod B to rod C [OK]
- Swapping auxiliary and destination rods
- Printing moves in wrong order
- Confusing disk numbers
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 0) {
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}Solution
Step 1: Analyze base case condition
Base case should stop recursion when n == 1, not 0, to print the last move.Step 2: Understand effect of current base case
With n == 0, no moves are printed for the smallest disk, causing no output.Final Answer:
Change base case condition to if (n == 1) instead of if (n == 0) -> Option AQuick Check:
Base case must be n == 1 to print moves [OK]
- Using n == 0 as base case
- Changing print statement unnecessarily
- Swapping rods incorrectly
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}Solution
Step 1: Understand requirement to print only disk 1 moves
We must print moves only when disk number is 1 inside the recursive function.Step 2: Modify print statements accordingly
Keep recursive calls intact but print only when n == 1, skipping other prints.Final Answer:
Add condition to print only if n == 1 inside the function -> Option BQuick Check:
Print condition inside recursion for disk 1 only [OK]
- Removing recursive calls breaks solution
- Filtering outside loses recursion context
- Changing base case incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
