{"id":"99089454-73e7-4976-b675-0e01a1dede81","name":"Rotate An Array","description":"1. You are given a number n, representing the size of array a.\r\n2. You are given n numbers, representing elements of array a.\r\n3. You are given a number k.\r\n4. Rotate the array a, k times to the right (for positive values of k), and to\r\nthe left for negative values of k.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"0 &lt;= n &lt; 10^4\r\n-10^9 &lt;= a[i], k &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<iostream>\r\nusing namespace std;\r\n\r\nvoid rotate(int* arr, int n, int k){\r\n // write your code here\r\n \r\n}\r\n\r\nvoid display(int* arr, int n){\r\n for(int i = 0 ; i < n; i++){\r\n cout<<arr[i]<<\" \";\r\n }\r\n cout<<endl;\r\n}\r\n\r\nint main(){\r\n int n, r;\r\n cin>>n;\r\n int* arr = new int[n];\r\n for(int i = 0 ; i < n; i++){\r\n cin>>arr[i];\r\n }\r\n cin>>r;\r\n \r\n rotate(arr,n,r);\r\n display(arr,n);\r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n public static void display(int[] a){\r\n StringBuilder sb = new StringBuilder();\r\n\r\n for(int val: a){\r\n sb.append(val + \" \");\r\n }\r\n System.out.println(sb);\r\n }\r\n\r\n public static void rotate(int[] a, int k){\r\n // write your code here\r\n }\r\n\r\npublic static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(br.readLine());\r\n int[] a = new int[n];\r\n for(int i = 0; i < n; i++){\r\n a[i] = Integer.parseInt(br.readLine());\r\n }\r\n int k = Integer.parseInt(br.readLine());\r\n\r\n rotate(a, k);\r\n display(a);\r\n }\r\n\r\n}"},"python":{"code":"def rotate(arr,n,k):\n # write your code here\n \ndef main():\n n=int(input())\n arr=[]\n for i in range(n):\n val=int(input())\n arr.append(val)\n k=int(input())\n arr = rotate(arr,n,k)\n for i in range(n):\n print(arr[i],end=\" \")\n\nmain()"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n1\r\n2\r\n3\r\n4\r\n5\r\n3","sampleOutput":"3 4 5 1 2","questionVideo":"https://www.youtube.com/embed/ZictKDbUWak","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":"8aada753-a2d2-4245-a028-624f817d5e90","name":"(Rotate an array) Rotate 458963 by k=-3","slug":"rotate-an-array-rotate-458963-by-k-3-luu21","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":"f10b54f1-0f44-408f-82d5-89c189f4ad57","name":"Function and Arrays","slug":"function-and-arrays","type":0},{"id":"bd14bfb6-40d6-455c-9e22-8d504c0e7087","name":"Rotate An Array","slug":"rotate-an-array","type":1}],"next":{"id":"06b6254b-96ff-439b-b09c-38c6a8ea3fa4","name":"Rotate an Array","type":3,"slug":"rotate-an-array"},"prev":{"id":"21bceb6d-67d7-4cd0-bcd8-4fd0de44363d","name":"Reverse an Array","type":3,"slug":"reverse-an-array"}}}

Rotate An Array

1. You are given a number n, representing the size of array a. 2. You are given n numbers, representing elements of array a. 3. You are given a number k. 4. Rotate the array a, k times to the right (for positive values of k), and to the left for negative values of k.

{"id":"99089454-73e7-4976-b675-0e01a1dede81","name":"Rotate An Array","description":"1. You are given a number n, representing the size of array a.\r\n2. You are given n numbers, representing elements of array a.\r\n3. You are given a number k.\r\n4. Rotate the array a, k times to the right (for positive values of k), and to\r\nthe left for negative values of k.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"0 &lt;= n &lt; 10^4\r\n-10^9 &lt;= a[i], k &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<iostream>\r\nusing namespace std;\r\n\r\nvoid rotate(int* arr, int n, int k){\r\n // write your code here\r\n \r\n}\r\n\r\nvoid display(int* arr, int n){\r\n for(int i = 0 ; i < n; i++){\r\n cout<<arr[i]<<\" \";\r\n }\r\n cout<<endl;\r\n}\r\n\r\nint main(){\r\n int n, r;\r\n cin>>n;\r\n int* arr = new int[n];\r\n for(int i = 0 ; i < n; i++){\r\n cin>>arr[i];\r\n }\r\n cin>>r;\r\n \r\n rotate(arr,n,r);\r\n display(arr,n);\r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n public static void display(int[] a){\r\n StringBuilder sb = new StringBuilder();\r\n\r\n for(int val: a){\r\n sb.append(val + \" \");\r\n }\r\n System.out.println(sb);\r\n }\r\n\r\n public static void rotate(int[] a, int k){\r\n // write your code here\r\n }\r\n\r\npublic static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(br.readLine());\r\n int[] a = new int[n];\r\n for(int i = 0; i < n; i++){\r\n a[i] = Integer.parseInt(br.readLine());\r\n }\r\n int k = Integer.parseInt(br.readLine());\r\n\r\n rotate(a, k);\r\n display(a);\r\n }\r\n\r\n}"},"python":{"code":"def rotate(arr,n,k):\n # write your code here\n \ndef main():\n n=int(input())\n arr=[]\n for i in range(n):\n val=int(input())\n arr.append(val)\n k=int(input())\n arr = rotate(arr,n,k)\n for i in range(n):\n print(arr[i],end=\" \")\n\nmain()"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n1\r\n2\r\n3\r\n4\r\n5\r\n3","sampleOutput":"3 4 5 1 2","questionVideo":"https://www.youtube.com/embed/ZictKDbUWak","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":"8aada753-a2d2-4245-a028-624f817d5e90","name":"(Rotate an array) Rotate 458963 by k=-3","slug":"rotate-an-array-rotate-458963-by-k-3-luu21","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":"f10b54f1-0f44-408f-82d5-89c189f4ad57","name":"Function and Arrays","slug":"function-and-arrays","type":0},{"id":"bd14bfb6-40d6-455c-9e22-8d504c0e7087","name":"Rotate An Array","slug":"rotate-an-array","type":1}],"next":{"id":"06b6254b-96ff-439b-b09c-38c6a8ea3fa4","name":"Rotate an Array","type":3,"slug":"rotate-an-array"},"prev":{"id":"21bceb6d-67d7-4cd0-bcd8-4fd0de44363d","name":"Reverse an Array","type":3,"slug":"reverse-an-array"}}}
plane

Editor


Loading...

Rotate An Array

easy

1. You are given a number n, representing the size of array a. 2. You are given n numbers, representing elements of array a. 3. You are given a number k. 4. Rotate the array a, k times to the right (for positive values of k), and to the left for negative values of k.

Constraints

0 <= n < 10^4 -10^9 <= a[i], k <= 10^9

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

5 1 2 3 4 5 3

Sample Output

3 4 5 1 2

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode