Problem of the day
You are given three rods (numbered 1 to 3), and ‘N’ disks initially placed on the first rod, one on top of each other in increasing order of size ( the largest disk is at the bottom). You are supposed to move the ‘N’ disks to another rod(either rod 2 or rod 3) using the following rules and in less than 2 ^ (N) moves.
1. You can only move one disk in one move.
2. You can not place a larger disk on top of a smaller disk.
3. You can only move the disk at the top of any rod.
Note :
You may assume that initially, the size of the ‘i’th disk from the top of the stack is equal to ‘i’, i.e. the disk at the bottom has size ‘N’, the disk above that has size ‘N - 1’, and so on. The disk at the top has size 1.
Example :
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases follow.
The first line of each test case contains a single integer ‘N’ denoting the number disks.
Output Format :
For each test case, return a 2-D array/list, where each row of the array should contain exactly two integers. The first integer should be the number of the rod from where the topmost disk is to be removed and the second integer should denote the number of the rod where the removed disk is to be placed. If you have correctly moved all the disks from rod 1 to either rod 2 or rod 3 the output will be ‘1’ otherwise the output will be ‘0’.
The output of every test case will be printed in a separate line.
Note :
You don’t need to print anything; It has already been taken care of.
1 <= T <= 5
1 <= N <= 12
Where ‘T’ denotes the number of test cases, ‘N’ denotes the number of disks.
Time Limit: 1 sec
2
1
2
1
1
In the first test case,
you can move the only disk to either rod 2 or rod 3. The matrix to be returned should be either { { 1, 2 } } or { {1, 3 } }.
In the second test case,
you can move the topmost disk from rod 1 to rod 2. Then move the remaining disk from rod1 to rod 3. Now move the disk in rod 2 to rod 3. . One of the correct ways to return the output is { { 1, 2 }, { 1, 3 }, { 2, 3 } }.
1
3
1
One of the correct matrices is { { 1, 2 }, { 1, 3 }, { 2, 3 }, { 1, 2 }, { 3, 1 }, { 3, 2 }, { 1, 2 } }.
Can you try to solve this problem for N = 1 and then use recursion to solve for N > 1?
The idea is to use recursion to solve the problem. Firstly, we will try to solve this problem for N = 2; we can move a disk from rod 1 to rod 2, then move another disk from rod 1 to rod 2, and then move the disk in rod 2 to rod 3, this way we can move all the disks to rod 3. Now, to solve for N = 3, we can first solve the problem for the first two disks and then move the last disk to another rod and then place the two disks on top of the last disk.
Generalizing this approach, to solve the problem for ‘N’ disks we will first solve the problem for ‘N-1’ disks and then move the last disk to another rod, thus completely solving the problem for ‘N’ disks.
The steps are as follows :
O(2 ^ N), where N is the number of disks.
We need exactly 2 ^ N -1 moves to shift N disks from one rod to another. Hence, the overall Time Complexity is O(2 ^ N).
O(N), where N is the number of disks.
We are using recursion to move disks and the maximum size of the recursion stack is N. Hence, the overall Space Complexity is O(N).