`{"id":"61088a75-6c3b-4433-ad9c-23a33d680210","name":"Ring Rotate","description":"You are given a n*m matrix where n are the number of rows and m are the number of columns. You are also given n*m numbers representing the elements of the matrix.\r\nYou will be given a ring number 's' representing the ring of the matrix. For details, refer to image.\r\n\r\n<img src=\"http://pepcoding.com/resources/ojquestionresource/images/shell-rotate.jpg\" alt=\"shell-rotate\">\r\n\r\nYou will be given a number 'r' representing number of rotations in an anti-clockwise manner of the specified ring.\r\nYou are required to rotate the 's'th ring by 'r' rotations and display the rotated matrix.","inputFormat":"A number n\r\nA number m\r\ne11\r\ne12..\r\ne21\r\ne22..\r\n.. n * m number of elements of array a\r\nA number s\r\nA number r","outputFormat":"output is handled by display function","constraints":"1 &lt;= n &lt;= 10^2\r\n1 &lt;= m &lt;= 10^2\r\n-10^9 &lt;= e11, e12, .. n * m elements &lt;= 10^9\r\n0 &lt; s &lt;= min(n, m) / 2\r\n-10^9 &lt;= r &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n\nvoid display(vector<vector<int>>&arr){\n \n for(int i=0;i<arr.size();i++){\n for(int j=0;j<arr[0].size();j++){\n cout<<arr[i][j]<<\" \";\n }\n cout<<endl;\n }\n}\n\nint main()\n{\n int n, m, s, r;\n cin >> n >> m;\n vector<vector<int>> arr(n, vector<int>(m));\n for (int i = 0; i < n; i++)\n for (int j = 0; j < m; j++)\n cin >> arr[i][j];\n cin >> s >> r;\n \n // write your code here\n \n \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[0].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 display(arr):\n for i in range(len(arr)):\n for j in range(len(arr[0])):\n print(arr[i][j], end = \" \")\n print(\"\")\n\n\nn = 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\ns= int(input())\nr= int(input())\n\n#write your code here"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n7\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n51\r\n52\r\n53\r\n54\r\n55\r\n56\r\n57\r\n2\r\n3","sampleOutput":"11 12 13 14 15 16 17\r\n21 25 26 36 46 45 27\r\n31 24 33 34 35 44 37\r\n41 23 22 32 42 43 47\r\n51 52 53 54 55 56 57","questionVideo":"https://www.youtube.com/embed/Eu1XAfmnGZ4","hints":[],"associated":[{"id":"51ac8858-f62e-412d-a066-51e27c9f7b24","name":"(Ring Rotate) What will be the space complexity for this code?","slug":"ring-rotate-what-will-be-the-space-complexity-for-this-code","type":4},{"id":"84e76bb0-e030-4f04-8d6c-2d8502527374","name":"(Ring Rotate) What is the number of elements in a particular shell?","slug":"ring-rotate-what-is-the-number-of-elements-in-a-particular-shell","type":4},{"id":"dd97b48a-70b5-491d-b77d-b3ee41a8fa6f","name":"(Ring rotate) What is the time complexity of this code?","slug":"ring-rotate-what-is-the-time-complexity-of-this-code","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":"72fae385-ab2a-4c33-ada2-04e3535629a8","name":"Ring Rotate","slug":"ring-rotate","type":1}],"next":{"id":"3e2481e7-d0e2-4740-92d8-e78255367492","name":"Ring Rotate","type":3,"slug":"ring-rotate"},"prev":{"id":"dc5a405a-ae4a-4966-91a5-34704a19faa3","name":"Rotate By 90 Degrees","type":3,"slug":"rotate-by-90-degrees"}}}`

# Ring Rotate

You are given a n*m matrix where n are the number of rows and m are the number of columns. You are also given n*m numbers representing the elements of the matrix. You will be given a ring number 's' representing the ring of the matrix. For details, refer to image. <img src="http://pepcoding.com/resources/ojquestionresource/images/shell-rotate.jpg" alt="shell-rotate"> You will be given a number 'r' representing number of rotations in an anti-clockwise manner of the specified ring. You are required to rotate the 's'th ring by 'r' rotations and display the rotated matrix.

`{"id":"61088a75-6c3b-4433-ad9c-23a33d680210","name":"Ring Rotate","description":"You are given a n*m matrix where n are the number of rows and m are the number of columns. You are also given n*m numbers representing the elements of the matrix.\r\nYou will be given a ring number 's' representing the ring of the matrix. For details, refer to image.\r\n\r\n<img src=\"http://pepcoding.com/resources/ojquestionresource/images/shell-rotate.jpg\" alt=\"shell-rotate\">\r\n\r\nYou will be given a number 'r' representing number of rotations in an anti-clockwise manner of the specified ring.\r\nYou are required to rotate the 's'th ring by 'r' rotations and display the rotated matrix.","inputFormat":"A number n\r\nA number m\r\ne11\r\ne12..\r\ne21\r\ne22..\r\n.. n * m number of elements of array a\r\nA number s\r\nA number r","outputFormat":"output is handled by display function","constraints":"1 &lt;= n &lt;= 10^2\r\n1 &lt;= m &lt;= 10^2\r\n-10^9 &lt;= e11, e12, .. n * m elements &lt;= 10^9\r\n0 &lt; s &lt;= min(n, m) / 2\r\n-10^9 &lt;= r &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n\nvoid display(vector<vector<int>>&arr){\n \n for(int i=0;i<arr.size();i++){\n for(int j=0;j<arr[0].size();j++){\n cout<<arr[i][j]<<\" \";\n }\n cout<<endl;\n }\n}\n\nint main()\n{\n int n, m, s, r;\n cin >> n >> m;\n vector<vector<int>> arr(n, vector<int>(m));\n for (int i = 0; i < n; i++)\n for (int j = 0; j < m; j++)\n cin >> arr[i][j];\n cin >> s >> r;\n \n // write your code here\n \n \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[0].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 display(arr):\n for i in range(len(arr)):\n for j in range(len(arr[0])):\n print(arr[i][j], end = \" \")\n print(\"\")\n\n\nn = 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\ns= int(input())\nr= int(input())\n\n#write your code here"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n7\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n51\r\n52\r\n53\r\n54\r\n55\r\n56\r\n57\r\n2\r\n3","sampleOutput":"11 12 13 14 15 16 17\r\n21 25 26 36 46 45 27\r\n31 24 33 34 35 44 37\r\n41 23 22 32 42 43 47\r\n51 52 53 54 55 56 57","questionVideo":"https://www.youtube.com/embed/Eu1XAfmnGZ4","hints":[],"associated":[{"id":"51ac8858-f62e-412d-a066-51e27c9f7b24","name":"(Ring Rotate) What will be the space complexity for this code?","slug":"ring-rotate-what-will-be-the-space-complexity-for-this-code","type":4},{"id":"84e76bb0-e030-4f04-8d6c-2d8502527374","name":"(Ring Rotate) What is the number of elements in a particular shell?","slug":"ring-rotate-what-is-the-number-of-elements-in-a-particular-shell","type":4},{"id":"dd97b48a-70b5-491d-b77d-b3ee41a8fa6f","name":"(Ring rotate) What is the time complexity of this code?","slug":"ring-rotate-what-is-the-time-complexity-of-this-code","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":"72fae385-ab2a-4c33-ada2-04e3535629a8","name":"Ring Rotate","slug":"ring-rotate","type":1}],"next":{"id":"3e2481e7-d0e2-4740-92d8-e78255367492","name":"Ring Rotate","type":3,"slug":"ring-rotate"},"prev":{"id":"dc5a405a-ae4a-4966-91a5-34704a19faa3","name":"Rotate By 90 Degrees","type":3,"slug":"rotate-by-90-degrees"}}}`

Editor

# Ring Rotate

easy

You are given a n*m matrix where n are the number of rows and m are the number of columns. You are also given n*m numbers representing the elements of the matrix. You will be given a ring number 's' representing the ring of the matrix. For details, refer to image. You will be given a number 'r' representing number of rotations in an anti-clockwise manner of the specified ring. You are required to rotate the 's'th ring by 'r' rotations and display the rotated matrix.

## Constraints

1 <= n <= 10^2 1 <= m <= 10^2 -10^9 <= e11, e12, .. n * m elements <= 10^9 0 < s <= min(n, m) / 2 -10^9 <= r <= 10^9

## Format

### Input

A number n A number m e11 e12.. e21 e22.. .. n * m number of elements of array a A number s A number r

### Output

output is handled 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;}5 7 11 12 13 14 15 16 17 21 22 23 24 25 26 27 31 32 33 34 35 36 37 41 42 43 44 45 46 47 51 52 53 54 55 56 57 2 3```

### 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 12 13 14 15 16 17 21 25 26 36 46 45 27 31 24 33 34 35 44 37 41 23 22 32 42 43 47 51 52 53 54 55 56 57```

Question Video

Discussions

Show Discussion

Related Resources