`{"id":"31cd5861-0459-48c1-a826-62b11916235b","name":"Tower Of Hanoi","description":"<p>Tower Of Hanoi</p><p>1. There are 3 towers. Tower 1 has n disks, where n is a positive number. Tower 2 and 3 are empty. 2. The disks are increasingly placed in terms of size such that the smallest disk is on top and largest disk is at bottom. 3. You are required to 3.1. Print the instructions to move the disks. 3.2. from tower 1 to tower 2 using tower 3 3.3. following the rules 3.3.1 move 1 disk at a time. 3.3.2 never place a smaller disk under a larger disk. 3.3.3 you can only move a disk at the top. Note -&gt; The online judge can't force you to write the function recursively but that is what the spirit of question is.Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.</p>","inputFormat":"<p>A number n, representing number of disks A number n1, representing id of tower 1 A number n2, representing id of tower 2 A number n3, representing id of tower 3</p>","outputFormat":"<p>n[n1 -&gt; n2] .. A set of instructions in above format to represent, move nth disc from n1 tower to n2 tower</p>","constraints":"<p>0 &lt;= n &lt;= 9 10 &lt;= n1, n2, n3 &lt;= 10^9 n1 != n2 != n3</p>","sampleCode":{"cpp":{"code":"#include<iostream>\r\nusing namespace std;\r\n\r\nvoid toh(int n, int t1id, int t2id, int t3id){\r\n // write your code here\r\n \r\n }\r\n\r\n\r\n\r\n\r\nint main() {\r\n\r\n int n;cin>>n;\r\n int n1,n2,n3;cin>>n1>>n2>>n3;\r\n toh(n, n1, n2, n3);\r\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public static void toh(int n, int t1id, int t2id, int t3id){\r\n \r\n }\r\n\r\n}"},"python":{"code":"def toh(n, t1, t2, t3):\r\n #write your code here\r\n\r\nn = int(input())\r\nn1,n2,n3 = int(input()),int(input()),int(input())\r\ntoh(n, n1, n2, n3);"}},"points":10,"difficulty":"easy","sampleInput":"3\r\n10\r\n11\r\n12","sampleOutput":"1[10 -> 11]\r\n2[10 -> 12]\r\n1[11 -> 12]\r\n3[10 -> 11]\r\n1[12 -> 10]\r\n2[12 -> 11]\r\n1[10 -> 11]","questionVideo":"https://www.youtube.com/embed/uwrc4H3yaJ4","hints":[],"associated":[{"id":"666a2265-6aa8-4e5c-b632-fe6c00bde1e2","name":"(Tower of Hanoi) Which of following is/are contained inside the rule-set of the Tower of Hanoi?","slug":"tower-of-hanoi-which-of-following-is-are-contained-inside-the-rule-set-of-the-tower-of-hanoi","type":4},{"id":"75be59de-3581-4336-9081-9ebee8e61174","name":"(Tower of Hanoi) it is possible to solve tower of hanoi with two rods, if","slug":"tower-of-hanoi-it-is-possible-to-solve-tower-of-hanoi-with-two-rods-if","type":4},{"id":"7fb0e5a5-2037-48b0-bfcd-8053402887e4","name":"(Tower of Hanoi) The recursion calls are managed inside which section of memory?","slug":"tower-of-hanoi-the-recursion-calls-are-managed-inside-which-section-of-memory","type":4},{"id":"89b33b78-d99f-4bf2-843e-83cc87af1e65","name":"(Tower of Hanoi) What do you mean by recursion?","slug":"tower-of-hanoi-what-do-you-mean-by-recursion","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"d341a7c9-1269-409c-b851-0bb512289544","name":"Recursion And Backtracking For Beginners","slug":"recursion-and-backtracking-for-beginners","type":0},{"id":"ebce38e2-8a4f-4c4e-93e3-68725a12c50d","name":"Tower Of Hanoi","slug":"tower-of-hanoi","type":1}],"next":{"id":"57655973-da51-40b6-a449-a97a98b974b9","name":"Tower of Hanoi","type":3,"slug":"tower-of-hanoi"},"prev":{"id":"ae000485-c119-4155-9b89-7fdfcaa1f489","name":"Print Zigzag","type":3,"slug":"print-zigzag"}}}`

# Tower Of Hanoi

<p>Tower Of Hanoi</p><p>1. There are 3 towers. Tower 1 has n disks, where n is a positive number. Tower 2 and 3 are empty. 2. The disks are increasingly placed in terms of size such that the smallest disk is on top and largest disk is at bottom. 3. You are required to 3.1. Print the instructions to move the disks. 3.2. from tower 1 to tower 2 using tower 3 3.3. following the rules 3.3.1 move 1 disk at a time. 3.3.2 never place a smaller disk under a larger disk. 3.3.3 you can only move a disk at the top. Note -&gt; The online judge can't force you to write the function recursively but that is what the spirit of question is.Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.</p>

`{"id":"31cd5861-0459-48c1-a826-62b11916235b","name":"Tower Of Hanoi","description":"<p>Tower Of Hanoi</p><p>1. There are 3 towers. Tower 1 has n disks, where n is a positive number. Tower 2 and 3 are empty. 2. The disks are increasingly placed in terms of size such that the smallest disk is on top and largest disk is at bottom. 3. You are required to 3.1. Print the instructions to move the disks. 3.2. from tower 1 to tower 2 using tower 3 3.3. following the rules 3.3.1 move 1 disk at a time. 3.3.2 never place a smaller disk under a larger disk. 3.3.3 you can only move a disk at the top. Note -&gt; The online judge can't force you to write the function recursively but that is what the spirit of question is.Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.</p>","inputFormat":"<p>A number n, representing number of disks A number n1, representing id of tower 1 A number n2, representing id of tower 2 A number n3, representing id of tower 3</p>","outputFormat":"<p>n[n1 -&gt; n2] .. A set of instructions in above format to represent, move nth disc from n1 tower to n2 tower</p>","constraints":"<p>0 &lt;= n &lt;= 9 10 &lt;= n1, n2, n3 &lt;= 10^9 n1 != n2 != n3</p>","sampleCode":{"cpp":{"code":"#include<iostream>\r\nusing namespace std;\r\n\r\nvoid toh(int n, int t1id, int t2id, int t3id){\r\n // write your code here\r\n \r\n }\r\n\r\n\r\n\r\n\r\nint main() {\r\n\r\n int n;cin>>n;\r\n int n1,n2,n3;cin>>n1>>n2>>n3;\r\n toh(n, n1, n2, n3);\r\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public static void toh(int n, int t1id, int t2id, int t3id){\r\n \r\n }\r\n\r\n}"},"python":{"code":"def toh(n, t1, t2, t3):\r\n #write your code here\r\n\r\nn = int(input())\r\nn1,n2,n3 = int(input()),int(input()),int(input())\r\ntoh(n, n1, n2, n3);"}},"points":10,"difficulty":"easy","sampleInput":"3\r\n10\r\n11\r\n12","sampleOutput":"1[10 -> 11]\r\n2[10 -> 12]\r\n1[11 -> 12]\r\n3[10 -> 11]\r\n1[12 -> 10]\r\n2[12 -> 11]\r\n1[10 -> 11]","questionVideo":"https://www.youtube.com/embed/uwrc4H3yaJ4","hints":[],"associated":[{"id":"666a2265-6aa8-4e5c-b632-fe6c00bde1e2","name":"(Tower of Hanoi) Which of following is/are contained inside the rule-set of the Tower of Hanoi?","slug":"tower-of-hanoi-which-of-following-is-are-contained-inside-the-rule-set-of-the-tower-of-hanoi","type":4},{"id":"75be59de-3581-4336-9081-9ebee8e61174","name":"(Tower of Hanoi) it is possible to solve tower of hanoi with two rods, if","slug":"tower-of-hanoi-it-is-possible-to-solve-tower-of-hanoi-with-two-rods-if","type":4},{"id":"7fb0e5a5-2037-48b0-bfcd-8053402887e4","name":"(Tower of Hanoi) The recursion calls are managed inside which section of memory?","slug":"tower-of-hanoi-the-recursion-calls-are-managed-inside-which-section-of-memory","type":4},{"id":"89b33b78-d99f-4bf2-843e-83cc87af1e65","name":"(Tower of Hanoi) What do you mean by recursion?","slug":"tower-of-hanoi-what-do-you-mean-by-recursion","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"d341a7c9-1269-409c-b851-0bb512289544","name":"Recursion And Backtracking For Beginners","slug":"recursion-and-backtracking-for-beginners","type":0},{"id":"ebce38e2-8a4f-4c4e-93e3-68725a12c50d","name":"Tower Of Hanoi","slug":"tower-of-hanoi","type":1}],"next":{"id":"57655973-da51-40b6-a449-a97a98b974b9","name":"Tower of Hanoi","type":3,"slug":"tower-of-hanoi"},"prev":{"id":"ae000485-c119-4155-9b89-7fdfcaa1f489","name":"Print Zigzag","type":3,"slug":"print-zigzag"}}}`

Editor

# Tower Of Hanoi

easy

Tower Of Hanoi

1. There are 3 towers. Tower 1 has n disks, where n is a positive number. Tower 2 and 3 are empty. 2. The disks are increasingly placed in terms of size such that the smallest disk is on top and largest disk is at bottom. 3. You are required to 3.1. Print the instructions to move the disks. 3.2. from tower 1 to tower 2 using tower 3 3.3. following the rules 3.3.1 move 1 disk at a time. 3.3.2 never place a smaller disk under a larger disk. 3.3.3 you can only move a disk at the top. Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is.Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.

## Constraints

0 <= n <= 9 10 <= n1, n2, n3 <= 10^9 n1 != n2 != n3

## Format

### Input

A number n, representing number of disks A number n1, representing id of tower 1 A number n2, representing id of tower 2 A number n3, representing id of tower 3

### Output

n[n1 -> n2] .. A set of instructions in above format to represent, move nth disc from n1 tower to n2 tower

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}3 10 11 12```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}1[10 -> 11] 2[10 -> 12] 1[11 -> 12] 3[10 -> 11] 1[12 -> 10] 2[12 -> 11] 1[10 -> 11]```

Question Video

Discussions

Show Discussion

Related Resources