# Arrays: Program to rotate an array to left

To rotate and array to left means to shift the elements to left.

The approach for the left rotation is an easy-to-understand method where the 'k' is the number of rotations that should be performed.

The array is rotated to left by shifting the elements of the arrays to the next element to its left in the array.

In left rotation, the first element of the array will be shifted to the last position in the array.

##### Approach

- To understand the concept better, let's take an example of an array of five elements array = {10, 20, 30, 40, 50}
- If we rotate this array to the left by one index, every element would be moved to the left by one place.
- The first element after moving to the left will come at the last place of the array. So after the left rotation

the array will look like array = {20, 30, 40, 50, 10}

##### Complexity

Time Complexity : O(n*k), where n is the number of elements in the array and k is the number of times we need to perform left rotation on the array

Since k can be at most n-1(k = k%n), the time complexity in the worst case is O(n^2)

Space Complexity : O(1) as we are not using extra space

##### Java Code

```
import java.util.Arrays;
class RotateLeft{
public static void leftRotation(int[] arr, int k) {
if(k == 0 || k % arr.length == 0)
return;
k = k % arr.length;
for(int i = 0 ; i < k ; i++) {
int first = arr[0]; //stores the first element
for(int j = 0 ; j < arr.length - 1 ; j++) {
arr[j] = arr[j+1]; //shifts by one position
}
arr[arr.length - 1] = first; //first element is added to last position
}
}
public static void main(String[] args) {
int[] array = {10, 20, 30, 40, 50};
int k = 1; // k is the number of times the array needs to be rotated to left
System.out.println("Original Array");
System.out.println(Arrays.toString(array));
leftRotation(array,k);
System.out.println("After Left Rotation " + k + " times");
System.out.println(Arrays.toString(array));
}
}
```

##### Output

Original Array

[10, 20, 30, 40, 50]

After Left Rotation 1 times

[20, 30, 40, 50, 10]