Tower of Hanoi | Set 2
Last Updated :
28 Feb, 2022
Given a positive integer N representing the number of disks in the Tower of Hanoi, the task is to solve the Tower of Hanoi puzzle using Binary representations.
Examples:
Input: N = 3
Output:
Move the 1 disk to next circular right rod
Move the 2 disk to next circular right rod
Move the 1 disk to next circular right rod
Move the 3 disk to next circular right rod
Move the 1 disk to next circular right rod
Move the 2 disk to next circular right rod
Move the 1 disk to next circular right rod
Input: N = 4
Output:
Move the 1 disk to next circular right rod
Move the 2 disk to next circular right rod
Move the 1 disk to next circular right rod
Move the 3 disk to next circular right rod
Move the 1 disk to next circular right rod
Move the 2 disk to next circular right rod
Move the 1 disk to next circular right rod
Move the 4 disk to next circular right rod
Move the 1 disk to next circular right rod
Move the 2 disk to next circular right rod
Move the 1 disk to next circular right rod
Move the 3 disk to next circular right rod
Move the 1 disk to next circular right rod
Move the 2 disk to next circular right rod
Move the 1 disk to next circular right rod
Approach: The given problem can be solved based on the following observations:
- It can be observed that to move the Nth disk, (N – 1)th disk needs to be moved. Therefore, to move (N – 1)th disk, (N – 2)th disk needs to be moved. This process goes on recursively.
- The above procedure is similar to setting the rightmost unset bit as it requires to set all the bits to right of it first.
- Therefore, the idea is to print all the intermediate steps, every time setting the right most bit by incrementing the current number by 1.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int increment( int * counter, int n)
{
int i = 0;
while ( true ) {
int a = counter[i] ^ 1;
int b = counter[i] & 1;
counter[i] = a;
if (b == 0)
break ;
i = i + 1;
}
return i;
}
void TowerOfHanoi( int N)
{
int counter[N] = { 0 };
for ( int step = 1;
step <= pow (2, N) - 1; step++) {
int x = increment(counter, N) + 1;
cout << "Move disk " << x
<< " to next circular"
<< " right rod \n" ;
}
}
int main()
{
int N = 3;
TowerOfHanoi(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int increment( int [] counter, int n)
{
int i = 0 ;
while ( true )
{
int a = counter[i] ^ 1 ;
int b = counter[i] & 1 ;
counter[i] = a;
if (b == 0 )
break ;
i = i + 1 ;
}
return i;
}
static void TowerOfHanoi( int N)
{
int [] counter= new int [N];
for ( int step = 1 ;
step <= Math.pow( 2 , N) - 1 ;
step++)
{
int x = increment(counter, N) + 1 ;
System.out.println( "Move disk " + x +
" to next circular" +
" right rod" );
}
}
public static void main(String[] args)
{
int N = 3 ;
TowerOfHanoi(N);
}
}
|
Python3
import math
def increment(counter, n):
i = 0
while ( True ):
a = counter[i] ^ 1
b = counter[i] & 1
counter[i] = a
if (b = = 0 ):
break
i = i + 1
return i
def TowerOfHanoi(N):
counter = [ 0 for i in range (N)]
for step in range ( 1 , int (math. pow ( 2 ,N))):
x = increment(counter, N) + 1
print ( "Move disk " ,x, " to next circular" , " right rod" )
N = 3
TowerOfHanoi(N)
|
C#
using System;
class GFG
{
static int increment( int [] counter, int n)
{
int i = 0;
while ( true )
{
int a = counter[i] ^ 1;
int b = counter[i] & 1;
counter[i] = a;
if (b == 0)
break ;
i = i + 1;
}
return i;
}
static void TowerOfHanoi( int N)
{
int [] counter = new int [N];
for ( int step = 1;
step <= ( int )(Math.Pow(2, N) - 1); step++)
{
int x = increment(counter, N) + 1;
Console.WriteLine( "Move disk " + x
+ " to next circular"
+ " right rod " );
}
}
public static void Main()
{
int N = 3;
TowerOfHanoi(N);
}
}
|
Javascript
<script>
function increment(counter, n)
{
let i = 0;
while ( true )
{
let a = counter[i] ^ 1;
let b = counter[i] & 1;
counter[i] = a;
if (b == 0)
break ;
i = i + 1;
}
return i;
}
function TowerOfHanoi(N)
{
let counter= Array.from({length: N}, (_, i) => 0);
for (let step = 1;
step <= Math.pow(2, N) - 1;
step++)
{
let x = increment(counter, N) + 1;
document.write( "Move disk " + x +
" to next circular" +
" right rod" + "<br/>" );
}
}
let N = 3;
TowerOfHanoi(N);
</script>
|
Output:
Move disk 1 to next circular right rod
Move disk 2 to next circular right rod
Move disk 1 to next circular right rod
Move disk 3 to next circular right rod
Move disk 1 to next circular right rod
Move disk 2 to next circular right rod
Move disk 1 to next circular right rod
Time Complexity: O(N * 2N)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized based on the observations that for M over the range [1, 2N – 1], source rod is equal to (m & (m – 1)) % 3 and the destination rod is equal to (m | (m – 1) + 1) % 3. Therefore, the idea is to iterate over the range [1, 2N – 1] and print the value of (i & (i – 1))%3 as source rod and (i | (i – 1) + 1)%3 as destination rod.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void TowerOfHanoi( int N)
{
for ( int x = 1;
x <= pow (2, N) - 1; x++) {
cout << "Move from Rod "
<< ((x & x - 1) % 3 + 1)
<< " to Rod "
<< (((x | x - 1) + 1) % 3 + 1)
<< endl;
}
}
int main()
{
int N = 3;
TowerOfHanoi(N);
return 0;
}
|
Java
class GFG{
static void TowerOfHanoi( int N)
{
for ( int x = 1 ;
x <= Math.pow( 2 , N) - 1 ; x++)
{
System.out.print( "Move from Rod " +
((x & x - 1 ) % 3 + 1 ) + " to Rod " +
(((x | x - 1 ) + 1 ) % 3 + 1 ) + "\n" );
}
}
public static void main (String[] args)
{
int N = 3 ;
TowerOfHanoi(N);
}
}
|
Python3
import math
def TowerOfHanoi(N):
for x in range ( 1 , int (math. pow ( 2 , N)) ):
print ( "Move from Rod " ,
((x & x - 1 ) % 3 + 1 ) , " to Rod " ,
(((x | x - 1 ) + 1 ) % 3 + 1 ) )
N = 3
TowerOfHanoi(N)
|
C#
using System;
class GFG{
static void TowerOfHanoi( int N)
{
for ( int x = 1;
x <= Math.Pow(2, N) - 1; x++)
{
Console.Write( "Move from Rod " +
((x & x - 1) % 3 + 1) + " to Rod " +
(((x | x - 1) + 1) % 3 + 1) + "\n" );
}
}
static void Main()
{
int N = 3;
TowerOfHanoi(N);
}
}
|
Javascript
<script>
function TowerOfHanoi(N)
{
for (let x = 1;
x <= Math.pow(2, N) - 1; x++) {
document.write( "Move from Rod "
+ ((x & x - 1) % 3 + 1)
+ " to Rod "
+ (((x | x - 1) + 1) % 3 + 1)
+ "<br>" );
}
}
let N = 3;
TowerOfHanoi(N);
</script>
|
Output:
Move from Rod 1 to Rod 3
Move from Rod 1 to Rod 2
Move from Rod 3 to Rod 2
Move from Rod 1 to Rod 3
Move from Rod 2 to Rod 1
Move from Rod 2 to Rod 3
Move from Rod 1 to Rod 3
Time Complexity: O(2N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...