Problem of the day
You are given an array consisting of 'N' elements and you need to perform 'Q' queries on the given array. Each query consists of an integer which tells the number of elements by which you need to left rotate the given array. For each query return the final array obtained after performing the left rotations.
Note:
Perform each query on the original array only i.e. every output should be according to the original order of elements.
Example:
Let the array be [1, 2, 3, 4, 5, 6] and the queries be {2, 4, 1}. For every query, we’ll perform the required number of left rotations on the array.
For the first query, rotate the given array to the left by 2 elements, so the resultant array is: [3, 4, 5, 6, 1, 2].
For the second query, rotate the given array to the left by 4 elements, so the resultant array is: [5, 6, 1, 2, 3, 4].
For the third query, rotate the given array to the left by 1 element, so the resultant array is: [2, 3, 4, 5, 6, 1].
The very first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of every test case contains two space-separated integers ‘N’ and ‘Q’ denoting the number of elements present in the array and the number of queries to be performed on the given array, respectively.
The second line of every test case contains ‘N’ space-separated integers denoting the elements present in the array.
The third line of every test case contains ‘Q’ space-separated integers denoting the queries.
Output format:
For every query of each test case, return ‘N’ space-separated integers which denote the resultant array after performing the rotation.
Note:
You don't need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 1000
1 <= Q <= 100
0 <= Queries[i] <= 10^5
-10^5 <= Array[i] <= 10^5
Where 'Queries[i]' denotes the extent to which the array in each query needs to be rotated and 'Array[i]' denotes the array element.
Time limit: 1 sec
2
5 3
7 8 6 1 2
8 4 3
2 2
12 15
1 2
1 2 7 8 6
2 7 8 6 1
1 2 7 8 6
15 12
12 15
In test case 1, we have, array: [7, 8, 6, 1, 2] and three queries: {8, 4, 3}.
For the first query we rotate the given array to the left 8 times, so the resultant array is: [1, 2, 7, 8, 6].
For the second query we rotate the given array to the left 4 times, so the resultant array is: [2, 7, 8, 6, 1].
For the third query we rotate the given array to the left 3 times, so the resultant array is: [1, 2, 7, 8, 6].
In test case 2, we have, array: [12, 15] and two queries: {1, 2}.
For the first query we rotate the given array to the left 1 time, so the resultant array is: [15, 12].
For the second query we rotate the given array to the left 2 times, so the resultant array is: [12, 15].
2
6 3
10 20 30 40 50 60
12 2 5
1 2
-15
100 89
10 20 30 40 50 60
30 40 50 60 10 20
60 10 20 30 40 50
-15
-15
In test case 1, we have, array: [10, 20, 30, 40, 50, 60] and three queries: {12, 2, 5}.
For the first query we rotate the given array to the left 12 times, so the resultant array is: [10, 20, 30, 40, 50, 60].
For the second query we rotate the given array to the left 2 times, so the resultant array is: [30, 40, 50, 60, 10, 20].
For the third query we rotate the given array to the left 5 times, so the resultant array is: [60, 10, 20, 30, 40, 50]
In test case 2, we have, array: [-15] and two queries: {100, 89}.
For the first query we rotate the given array to the left 100 times, so the resultant array is: [-15].
For the second query we rotate the given array to the left 89 times, so the resultant array is: [-15].
A simple and intuitive approach could be to rotate the array one element at a time and repeat this step until we get the required number of rotations.
The idea is to create a function which would rotate the array one element at a time. This can be done by shifting the array towards left by one element and copying the first element to the end of the array. For every query repeatedly call the above function, until the desired rotation is obtained.
If the number of rotations required, say ‘K’, is greater than the number of elements, ‘N’, then the effective number of rotations will be ‘K’ % ‘N’. This is due to the fact that rotating the array by its size results in the same array.
An important point to note here that for every query, we need to perform the rotations on a temporary copy of the original array, so as to avoid any modifications on the original one. This is because we want to obtain the rotations of the original array, hence, we need the original array to remain unchanged.
O(N * K * Q), Where ‘N’ is the number of elements in the array, ‘Q’ is the number of queries and ‘K’ is the maximum number of rotations required, considering all the queries.
Since we rotate the array by K elements, each rotation requires N operations. Also, N operations to copy the original array into a temporary array. Hence the overall time complexity for Q queries will be O((N + N) * K * Q) = O(N * K * Q).
O(N).
Since O(N) extra space is required for storing the temporary array. Hence the space complexity will be O(N).