`{"id":"3ceec1d4-6e84-4672-a925-a13cabdf4e4e","name":"Rotate By 90 Degree","description":"1. You are given a number n, representing the number of rows and number of columns.\r\n2. You are given n*n numbers, representing elements of 2d array a.\r\n3. You are required to rotate the matrix by 90 degree clockwise and then display the contents using display function.\r\n*Note - you are required to do it in-place i.e. no extra space should be used to achieve it .*","inputFormat":"A number n\r\ne11\r\ne12..\r\ne21\r\ne22..\r\n.. n * n number of elements","outputFormat":"output is taken care of by display function","constraints":"1 &lt;= n &lt;= 10^2\r\n-10^9 &lt;= e1, e2, .. n * n elements &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nvoid rotate(int n, vector<vector<int>> &arr)\n{\n // write your code here\n}\n\nint main()\n{\n int n;\n cin >> n;\n vector<vector<int>> arr(n, vector<int>(n));\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n cin >> arr[i][j];\n rotate(n, arr);\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 display(int[][] arr){\r\n for(int i = 0; i < arr.length; i++){\r\n for(int j = 0; j < arr.length; j++){\r\n System.out.print(arr[i][j] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n }\r\n\r\n}"},"python":{"code":"def transpose(arr):\n #write your code here\n \n\ndef colSwap(arr):\n # write your code here\n\n\ndef display(arr):\n for i in range(0 , n):\n for j in range(0, n):\n print(arr[i][j], end = \" \")\n print(\"\")\n\n\nn= int(input())\narr = []\nfor i in range(n): \n a=[]\n for j in range(n):\n a.append(int(input()));\n arr.append(a)\n\ntranspose(arr)\ncolSwap(arr)\ndisplay(arr)"}},"points":10,"difficulty":"easy","sampleInput":"4\r\n11\r\n12\r\n13\r\n14\r\n21\r\n22\r\n23\r\n24\r\n31\r\n32\r\n33\r\n34\r\n41\r\n42\r\n43\r\n44","sampleOutput":"41 31 21 11\r\n42 32 22 12\r\n43 33 23 13\r\n44 34 24 14","questionVideo":"https://www.youtube.com/embed/0qj4kRcuKqo","hints":[],"associated":[{"id":"2d87e488-1197-4f25-8548-465b1f1a823c","name":"(Rotate) What will be the time complexity of the following code?","slug":"rotate-what-will-be-the-time-complexity-of-the-following-code","type":4},{"id":"5d6be83b-956f-4b14-bed6-82ad104cc3c3","name":"(Rotate) What will be the auxiliary space complexity of the following code?","slug":"rotate-what-will-be-the-auxiliary-space-complexity-of-the-following-code","type":4},{"id":"be92d2a0-32cb-4abf-8517-46502163d5ae","name":"(Rotate) To rotate an array by using the algorithm of rotating its elements one by one is an in place algorithm.","slug":"rotate-to-rotate-an-array-by-using-the-algorithm-of-rotating-its-elements-one-by-one-is-an-in-place-algorithm","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":"6220bf67-464e-4408-a4c0-3d2d0894e65f","name":"Rotate By 90 Degree","slug":"rotate-by-90-degree","type":1}],"next":{"id":"dc5a405a-ae4a-4966-91a5-34704a19faa3","name":"Rotate By 90 Degrees","type":3,"slug":"rotate-by-90-degrees"},"prev":{"id":"362c4ba8-b971-4aa8-8d30-a6c3b9768b0e","name":"Exit Point of a Matrix","type":3,"slug":"exit-point-of-a-matrix"}}}`

# Rotate By 90 Degree

1. You are given a number n, representing the number of rows and number of columns. 2. You are given n*n numbers, representing elements of 2d array a. 3. You are required to rotate the matrix by 90 degree clockwise and then display the contents using display function. *Note - you are required to do it in-place i.e. no extra space should be used to achieve it .*

`{"id":"3ceec1d4-6e84-4672-a925-a13cabdf4e4e","name":"Rotate By 90 Degree","description":"1. You are given a number n, representing the number of rows and number of columns.\r\n2. You are given n*n numbers, representing elements of 2d array a.\r\n3. You are required to rotate the matrix by 90 degree clockwise and then display the contents using display function.\r\n*Note - you are required to do it in-place i.e. no extra space should be used to achieve it .*","inputFormat":"A number n\r\ne11\r\ne12..\r\ne21\r\ne22..\r\n.. n * n number of elements","outputFormat":"output is taken care of by display function","constraints":"1 &lt;= n &lt;= 10^2\r\n-10^9 &lt;= e1, e2, .. n * n elements &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nvoid rotate(int n, vector<vector<int>> &arr)\n{\n // write your code here\n}\n\nint main()\n{\n int n;\n cin >> n;\n vector<vector<int>> arr(n, vector<int>(n));\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n cin >> arr[i][j];\n rotate(n, arr);\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 display(int[][] arr){\r\n for(int i = 0; i < arr.length; i++){\r\n for(int j = 0; j < arr.length; j++){\r\n System.out.print(arr[i][j] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n }\r\n\r\n}"},"python":{"code":"def transpose(arr):\n #write your code here\n \n\ndef colSwap(arr):\n # write your code here\n\n\ndef display(arr):\n for i in range(0 , n):\n for j in range(0, n):\n print(arr[i][j], end = \" \")\n print(\"\")\n\n\nn= int(input())\narr = []\nfor i in range(n): \n a=[]\n for j in range(n):\n a.append(int(input()));\n arr.append(a)\n\ntranspose(arr)\ncolSwap(arr)\ndisplay(arr)"}},"points":10,"difficulty":"easy","sampleInput":"4\r\n11\r\n12\r\n13\r\n14\r\n21\r\n22\r\n23\r\n24\r\n31\r\n32\r\n33\r\n34\r\n41\r\n42\r\n43\r\n44","sampleOutput":"41 31 21 11\r\n42 32 22 12\r\n43 33 23 13\r\n44 34 24 14","questionVideo":"https://www.youtube.com/embed/0qj4kRcuKqo","hints":[],"associated":[{"id":"2d87e488-1197-4f25-8548-465b1f1a823c","name":"(Rotate) What will be the time complexity of the following code?","slug":"rotate-what-will-be-the-time-complexity-of-the-following-code","type":4},{"id":"5d6be83b-956f-4b14-bed6-82ad104cc3c3","name":"(Rotate) What will be the auxiliary space complexity of the following code?","slug":"rotate-what-will-be-the-auxiliary-space-complexity-of-the-following-code","type":4},{"id":"be92d2a0-32cb-4abf-8517-46502163d5ae","name":"(Rotate) To rotate an array by using the algorithm of rotating its elements one by one is an in place algorithm.","slug":"rotate-to-rotate-an-array-by-using-the-algorithm-of-rotating-its-elements-one-by-one-is-an-in-place-algorithm","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":"6220bf67-464e-4408-a4c0-3d2d0894e65f","name":"Rotate By 90 Degree","slug":"rotate-by-90-degree","type":1}],"next":{"id":"dc5a405a-ae4a-4966-91a5-34704a19faa3","name":"Rotate By 90 Degrees","type":3,"slug":"rotate-by-90-degrees"},"prev":{"id":"362c4ba8-b971-4aa8-8d30-a6c3b9768b0e","name":"Exit Point of a Matrix","type":3,"slug":"exit-point-of-a-matrix"}}}` Editor

# Rotate By 90 Degree

easy

1. You are given a number n, representing the number of rows and number of columns. 2. You are given n*n numbers, representing elements of 2d array a. 3. You are required to rotate the matrix by 90 degree clockwise and then display the contents using display function. *Note - you are required to do it in-place i.e. no extra space should be used to achieve it .*

## Constraints

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

## Format

### Input

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

### Output

output is taken care of by display function

## 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;}4 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44```

### 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;}41 31 21 11 42 32 22 12 43 33 23 13 44 34 24 14```

Question Video

Discussions

Show Discussion

Related Resources 