Tower of Hanoi Problem Statement

You have three rods numbered from 1 to 3, and 'N' disks initially stacked on the first rod in increasing order of their sizes (largest disk at the bottom). Your task is to move the 'N' disks to another rod (either rod 2 or rod 3) by following the rules below in less than 2N moves.

1. Only one disk can be moved at a time. 
2. Larger disks cannot be placed on top of smaller disks. 
3. Disks can only be moved from the top of a rod.
Note:
The initial size of the 'i'th disk from the top of the stack is equal to 'i', meaning the bottom disk has size 'N', the disk above it has size 'N-1', and so forth, with the top disk being size 1.

Example:

Example

Input:

The first line contains an integer 'T', the number of test cases. Each test case contains a single integer 'N', representing the number of disks.

Output:

For each test case, output a 2-D array/list where each row contains two integers. The first integer is the number of the rod from which the topmost disk is moved, and the second integer is the rod number to which the disk is placed. The result is '1' if all disks are successfully moved to rod 2 or 3, otherwise, '0'.
Note:
Printing is not required; simply implement the function to return the answer.

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 12
  • 'T' represents the number of test cases, 'N' represents the number of disks.
  • Time Limit: 1 second
Be the first one to answer
Add answer anonymously...
MakeMyTrip Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter