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].
Input format:
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.
Constraints:
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
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 firs...read more
Instead of rotating the array one element at a time, a complete block of array can be rotated. Suppose for a given query we need to output the array obtained after ‘K’ left rotat...read more
The previous approaches required us to perform actual rotations for every query. Also in order to avoid any modifications to the original array, we needed to copy the original a...read more
Instead of using a temporary array of size 2 * ‘N’, we can generate the rotated array directly from the given array, using just the indices.
Suppose for a given query we need to out...read more
Top Tech Vedika Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Reviews
Interviews
Salaries
Users/Month