# Left Rotations of An Array

Posted: 26 Dec, 2020
Difficulty: Moderate

## PROBLEM STATEMENT

#### 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
`````` Approach 1

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.