Professional Documents
Culture Documents
Ans:
Steps:
1) Move Smallest disk to the right most peg (at index right ). Similarly, 2 nd smallest disk to 2nd
right most peg and so on...
2) Move the right most disk to the left most empty (at index leftEmpty) peg. And increase the
leftEmpty = leftEmpty + 1.
3) Right shift every disk by 1 peg which are at index leftEmpty+1 to shift (start shifting from
right side).
4) Repeat step 2 & 3 until right most peg contains largest disk.
5) Now start moving disk from right side (start from the second right most disk) to the right
most(destination) peg.
Time Complexity:
4(n^2) + 2n – 4 / 2
2(n^2) + n - 2
O(n^2)
2)
Steps:
1) Move Smallest disk to the right most peg (at index right ). Similarly, 2 nd smallest disk to 2nd
right most peg and so on...
2) Now move all disks(Except largest) starting from 2 nd largest to the 1st peg.
Total no. of moves: (2 + … + n-1 + n) + (2 + … + n-1) + … 2
3) Now Shift the next largest disk to the Destination(k th) peg.
Total no. of moves: (n-1) + (n-1) + …(n-1)times
4) Repeat steps 1,2 & 3 until right most peg have n-1 disks.
5) Move 1st(Smallest) disk to the right most(destination) peg.