`{"id":"2d92cd21-1fa0-4044-9eb9-07763bae7ccf","name":"Spiral Display","description":"1. You are given a number n, representing the number of rows.\r\n2. You are given a number m, representing the number of columns.\r\n3. You are given n*m numbers, representing elements of 2d array a.\r\n4. You are required to traverse and print the contents of the 2d array in form of a spiral.\r\nNote - Please check the sample output for details.","inputFormat":"A number n\r\nA number m\r\ne11\r\ne12..\r\ne21\r\ne22..\r\n.. n * m number of elements","outputFormat":"e11\r\ne21\r\n..\r\nen1\r\nen2\r\nen3\r\n..\r\nenm\r\ne(n-1)m\r\n..\r\ne1m\r\ne1(m-1)\r\n..\r\ne12\r\ne22\r\ne32\r\n..","constraints":"1 &lt;= n &lt;= 10^2\r\n1 &lt;= m &lt;= 10^2\r\n-10^9 &lt;= e1, e2, .. n * m elements &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <iostream>\nusing namespace std;\n\nconst int mr = 100, mc = 100;\nvoid inputBound(int (&mat)[mr][mc], int n, int m)\n{\n\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < m; j++)\n {\n cin >> mat[i][j];\n }\n }\n}\n\nint main(int argc, char** argv)\n{\n\n\n int mat[mr][mc] = {0};\n int n, m;\n cin >> n >> m;\n\n inputBound(mat, n, m);\n\n // write your code here"},"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}"},"python":{"code":"n= int(input())\nm= int(input())\narr = []\nfor i in range(n): \n a=[]\n for j in range(m):\n a.append(int(input()));\n arr.append(a)\n\n# write your code here"}},"points":10,"difficulty":"easy","sampleInput":"3\r\n5\r\n11\r\n12\r\n13\r\n14\r\n15\r\n21\r\n22\r\n23\r\n24\r\n25\r\n31\r\n32\r\n33\r\n34\r\n35","sampleOutput":"11\r\n21\r\n31\r\n32\r\n33\r\n34\r\n35\r\n25\r\n15\r\n14\r\n13\r\n12\r\n22\r\n23\r\n24","questionVideo":"https://www.youtube.com/embed/UPEZBPh1UcU","hints":[],"associated":[{"id":"16d9b09e-9914-4108-bebe-b6727f96cc0e","name":"(Spiral Display) What is the space complexity of solving spiral display?","slug":"spiral-display-what-is-the-space-complexity-of-solving-spiral-display","type":4},{"id":"264369c8-c0d5-4452-94a7-b60ac3123dcc","name":"(Spiral Display) What is the time complexity of solving spiral display?","slug":"spiral-display-what-is-the-time-complexity-of-solving-spiral-display","type":4},{"id":"da221a28-c9fb-41c3-9cc1-02b7aea908c6","name":"(Spiral Display) Which approach can be used to solve spiral display?","slug":"spiral-display-which-approach-can-be-used-to-solve-spiral-display","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":"f185374c-5448-476e-93e8-c4a4e9d1d520","name":"2D Arrays","slug":"2d-arrays-9999","type":0},{"id":"4543cbab-fdf5-4980-bd90-574084b82dad","name":"Spiral Display","slug":"spiral-display","type":1}],"next":{"id":"07eb9868-2299-40ad-8c64-d920da732845","name":"Spiral Display","type":3,"slug":"spiral-display"},"prev":{"id":"d12f6857-1963-4fd6-af7b-e28a5f8e659c","name":"The State Of Wakanda -1","type":3,"slug":"the-state-of-wakanda-1"}}}`

# Spiral Display

1. You are given a number n, representing the number of rows. 2. You are given a number m, representing the number of columns. 3. You are given n*m numbers, representing elements of 2d array a. 4. You are required to traverse and print the contents of the 2d array in form of a spiral. Note - Please check the sample output for details.

`{"id":"2d92cd21-1fa0-4044-9eb9-07763bae7ccf","name":"Spiral Display","description":"1. You are given a number n, representing the number of rows.\r\n2. You are given a number m, representing the number of columns.\r\n3. You are given n*m numbers, representing elements of 2d array a.\r\n4. You are required to traverse and print the contents of the 2d array in form of a spiral.\r\nNote - Please check the sample output for details.","inputFormat":"A number n\r\nA number m\r\ne11\r\ne12..\r\ne21\r\ne22..\r\n.. n * m number of elements","outputFormat":"e11\r\ne21\r\n..\r\nen1\r\nen2\r\nen3\r\n..\r\nenm\r\ne(n-1)m\r\n..\r\ne1m\r\ne1(m-1)\r\n..\r\ne12\r\ne22\r\ne32\r\n..","constraints":"1 &lt;= n &lt;= 10^2\r\n1 &lt;= m &lt;= 10^2\r\n-10^9 &lt;= e1, e2, .. n * m elements &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <iostream>\nusing namespace std;\n\nconst int mr = 100, mc = 100;\nvoid inputBound(int (&mat)[mr][mc], int n, int m)\n{\n\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < m; j++)\n {\n cin >> mat[i][j];\n }\n }\n}\n\nint main(int argc, char** argv)\n{\n\n\n int mat[mr][mc] = {0};\n int n, m;\n cin >> n >> m;\n\n inputBound(mat, n, m);\n\n // write your code here"},"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}"},"python":{"code":"n= int(input())\nm= int(input())\narr = []\nfor i in range(n): \n a=[]\n for j in range(m):\n a.append(int(input()));\n arr.append(a)\n\n# write your code here"}},"points":10,"difficulty":"easy","sampleInput":"3\r\n5\r\n11\r\n12\r\n13\r\n14\r\n15\r\n21\r\n22\r\n23\r\n24\r\n25\r\n31\r\n32\r\n33\r\n34\r\n35","sampleOutput":"11\r\n21\r\n31\r\n32\r\n33\r\n34\r\n35\r\n25\r\n15\r\n14\r\n13\r\n12\r\n22\r\n23\r\n24","questionVideo":"https://www.youtube.com/embed/UPEZBPh1UcU","hints":[],"associated":[{"id":"16d9b09e-9914-4108-bebe-b6727f96cc0e","name":"(Spiral Display) What is the space complexity of solving spiral display?","slug":"spiral-display-what-is-the-space-complexity-of-solving-spiral-display","type":4},{"id":"264369c8-c0d5-4452-94a7-b60ac3123dcc","name":"(Spiral Display) What is the time complexity of solving spiral display?","slug":"spiral-display-what-is-the-time-complexity-of-solving-spiral-display","type":4},{"id":"da221a28-c9fb-41c3-9cc1-02b7aea908c6","name":"(Spiral Display) Which approach can be used to solve spiral display?","slug":"spiral-display-which-approach-can-be-used-to-solve-spiral-display","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":"f185374c-5448-476e-93e8-c4a4e9d1d520","name":"2D Arrays","slug":"2d-arrays-9999","type":0},{"id":"4543cbab-fdf5-4980-bd90-574084b82dad","name":"Spiral Display","slug":"spiral-display","type":1}],"next":{"id":"07eb9868-2299-40ad-8c64-d920da732845","name":"Spiral Display","type":3,"slug":"spiral-display"},"prev":{"id":"d12f6857-1963-4fd6-af7b-e28a5f8e659c","name":"The State Of Wakanda -1","type":3,"slug":"the-state-of-wakanda-1"}}}`

Editor

# Spiral Display

easy

1. You are given a number n, representing the number of rows. 2. You are given a number m, representing the number of columns. 3. You are given n*m numbers, representing elements of 2d array a. 4. You are required to traverse and print the contents of the 2d array in form of a spiral. Note - Please check the sample output for details.

## Constraints

1 <= n <= 10^2 1 <= m <= 10^2 -10^9 <= e1, e2, .. n * m elements <= 10^9

## Format

### Input

A number n A number m e11 e12.. e21 e22.. .. n * m number of elements

### Output

e11 e21 .. en1 en2 en3 .. enm e(n-1)m .. e1m e1(m-1) .. e12 e22 e32 ..

## 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 5 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35```

### 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;}11 21 31 32 33 34 35 25 15 14 13 12 22 23 24```

Question Video

Discussions

Show Discussion

Related Resources