Rotate an array to the

`k`

positions `k`

can be greater than the size of the array.Example:

`rotateLeft({1,2,3,4,5},2) --> {3,4,5,1,2}`

Need a **hand?** Try out these hints, one at a time.

Take care of the fact that the number

`k`

can be greater than the size of the array. Use the modulo (%) operator to find the actual number of shifts needed.
If you want to avoid an **O(n**^{2}) runtime, there's a neat little trick that will help you rotate the array in **O(n)** time. The algorithm is as follows :

1) Reverse the entire array.

2) Reverse the left portion of the array.

3) Reverse the right portion of the array.

The key is to accurately determine where the array should be segmented when reversing the left and right portions.

1) Reverse the entire array.

2) Reverse the left portion of the array.

3) Reverse the right portion of the array.

The key is to accurately determine where the array should be segmented when reversing the left and right portions.

1. Find the actual number of elements that need to be moved to the end of the array - **%** is the Modulo operator.

2. Reverse the entire array

3. Reverse the first half of the array from

4. Reverse the second half of the array from

`actualShifts = k % size`

. 2. Reverse the entire array

3. Reverse the first half of the array from

`0`

to `sz - actual_shifts-1`

4. Reverse the second half of the array from

`sz - actual_shifts`

to `sz-1`

public static int[] rotateLeft(int[] arr, int k) { }

**C**

**Java**

**Python**