-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram62.java
More file actions
24 lines (20 loc) · 835 Bytes
/
Program62.java
File metadata and controls
24 lines (20 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Program62 {
// Tower of Hanoi problem using recursion
public static void towerOfHanoi(int n, String src, String helper, String dest) {
if (n == 1) {
System.out.println("Move disk " + n + " from " + src + " to " + dest);
return;
}
// Move the first n-1 disks from source to helper
towerOfHanoi(n - 1, src, dest, helper);
// Move the nth disk from source to destination
System.out.println("Move disk " + n + " from " + src + " to " + dest);
// Move the n-1 disks from helper to destination
towerOfHanoi(n - 1, helper, src, dest);
}
// time complexity: O(2^n)
public static void main(String[] args) {
int n = 2; // Number of disks
towerOfHanoi(n, "src", "helper", "dest");
}
}