{"id":"f7207375-5cf2-4aaf-b7c5-09914b72e988","name":"Rotate Image","description":"1. You are given an n x n 2D matrix representing an image.\r\n2. rotate the image by 90 degrees (clockwise).\r\n3. You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. \r\n4. DO NOT allocate another 2D matrix and do the rotation.\r\n","inputFormat":"matrix = {\r\n {11, 12, 13, 14},\r\n {21, 22, 23, 24},\r\n {31, 32, 33, 34},\r\n {41, 42, 43, 44}\r\n}","outputFormat":"matrix = {\r\n {41, 31, 21, 11},\r\n {42, 32, 22, 12},\r\n {43, 33, 23, 13},\r\n {44, 34, 24, 14}\r\n}\r\n","constraints":"1. matrix.length == n\r\n2. matrix[i].length == n\r\n3. 1 &lt;= n &lt;= 20\r\n4. -1000 &lt;= matrix[i][j] &lt;= 1000\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid rotate(vector<vector<int>>& matrix)\n{\n //write your code here\n}\n\n\nint main()\n{\n int n;\n cin>>n;\n vector<vector<int>> matrix(n,vector<int> (n));\n\n \n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n cin>>matrix[i][j];\n }\n }\n \n rotate(matrix);\n \n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n cout<<matrix[i][j]<<\" \";\n }\n cout<<endl;\n }\n \n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~User Section~~~~~~~~~~~~~\r\n public static void rotate(int[][] matrix) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~Input Management~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[][] matrix = new int[n][n];\r\n for (int i = 0; i < n; i++) {\r\n for (int j = 0; j < n; j++) {\r\n matrix[i][j] = scn.nextInt();\r\n }\r\n }\r\n rotate(matrix);\r\n for (int i = 0; i < n; i++) {\r\n for (int j = 0; j < n; j++) {\r\n System.out.print(matrix[i][j] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n11 12 13\r\n21 22 23\r\n31 32 33","sampleOutput":"31 21 11 \r\n32 22 12 \r\n33 23 13 ","questionVideo":"","hints":[],"associated":[],"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":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"1f67f3f0-cb2e-459b-a75f-a73fb18f53f8","name":"Rotate Image","slug":"rotate-image","type":1}],"next":{"id":"69c1a6c6-3fe5-4226-b737-e745febdaaa6","name":"Rotate Image","type":3,"slug":"rotate-image"},"prev":{"id":"f53e0689-cd02-40a0-bd48-8b88f84789ff","name":"Shortest Unsorted Continuous Subarray MCQ","type":0,"slug":"shortest-unsorted-continuous-subarray-mcq"}}}

Rotate Image

1. You are given an n x n 2D matrix representing an image. 2. rotate the image by 90 degrees (clockwise). 3. You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. 4. DO NOT allocate another 2D matrix and do the rotation.

{"id":"f7207375-5cf2-4aaf-b7c5-09914b72e988","name":"Rotate Image","description":"1. You are given an n x n 2D matrix representing an image.\r\n2. rotate the image by 90 degrees (clockwise).\r\n3. You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. \r\n4. DO NOT allocate another 2D matrix and do the rotation.\r\n","inputFormat":"matrix = {\r\n {11, 12, 13, 14},\r\n {21, 22, 23, 24},\r\n {31, 32, 33, 34},\r\n {41, 42, 43, 44}\r\n}","outputFormat":"matrix = {\r\n {41, 31, 21, 11},\r\n {42, 32, 22, 12},\r\n {43, 33, 23, 13},\r\n {44, 34, 24, 14}\r\n}\r\n","constraints":"1. matrix.length == n\r\n2. matrix[i].length == n\r\n3. 1 &lt;= n &lt;= 20\r\n4. -1000 &lt;= matrix[i][j] &lt;= 1000\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid rotate(vector<vector<int>>& matrix)\n{\n //write your code here\n}\n\n\nint main()\n{\n int n;\n cin>>n;\n vector<vector<int>> matrix(n,vector<int> (n));\n\n \n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n cin>>matrix[i][j];\n }\n }\n \n rotate(matrix);\n \n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n cout<<matrix[i][j]<<\" \";\n }\n cout<<endl;\n }\n \n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~User Section~~~~~~~~~~~~~\r\n public static void rotate(int[][] matrix) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~Input Management~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[][] matrix = new int[n][n];\r\n for (int i = 0; i < n; i++) {\r\n for (int j = 0; j < n; j++) {\r\n matrix[i][j] = scn.nextInt();\r\n }\r\n }\r\n rotate(matrix);\r\n for (int i = 0; i < n; i++) {\r\n for (int j = 0; j < n; j++) {\r\n System.out.print(matrix[i][j] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n11 12 13\r\n21 22 23\r\n31 32 33","sampleOutput":"31 21 11 \r\n32 22 12 \r\n33 23 13 ","questionVideo":"","hints":[],"associated":[],"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":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"1f67f3f0-cb2e-459b-a75f-a73fb18f53f8","name":"Rotate Image","slug":"rotate-image","type":1}],"next":{"id":"69c1a6c6-3fe5-4226-b737-e745febdaaa6","name":"Rotate Image","type":3,"slug":"rotate-image"},"prev":{"id":"f53e0689-cd02-40a0-bd48-8b88f84789ff","name":"Shortest Unsorted Continuous Subarray MCQ","type":0,"slug":"shortest-unsorted-continuous-subarray-mcq"}}}
plane

Editor


Loading...

Rotate Image

easy

1. You are given an n x n 2D matrix representing an image. 2. rotate the image by 90 degrees (clockwise). 3. You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. 4. DO NOT allocate another 2D matrix and do the rotation.

Constraints

1. matrix.length == n 2. matrix[i].length == n 3. 1 <= n <= 20 4. -1000 <= matrix[i][j] <= 1000

Format

Input

matrix = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34}, {41, 42, 43, 44} }

Output

matrix = { {41, 31, 21, 11}, {42, 32, 22, 12}, {43, 33, 23, 13}, {44, 34, 24, 14} }

Example

Sample Input

3 11 12 13 21 22 23 31 32 33

Sample Output

31 21 11 32 22 12 33 23 13

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode