## Left Rotate of an Array

Back to Programming

### Description

The elements are taken as the input. The array is rotated left. That means, the element of index 1 is stored in index 0, the element of index 2 is stored in index 1 and so on. And the element of index 0 is stored in index n-1..

Let the array be:

0                                                          1                                                  2                                                 3                                                     4

First the element of index 0 (here, 14) will be stored in a temporary variable.

0                                    1                                 2                                     3                                 4

Now the second element will be copied to the first index making the array as:

0                                    1                                 2                                     3                                 4

By the same process, the third element will be copied to the 2nd position and so on till the last element of the array will be stored at the 2nd last position of the array.

The array will be:

0                                                       1                                                       2                                                  3                                                   4

At last the value of the temporary element will be stored at the last index so the array will be:

0                                                             1                                                   2                                                3                                                4

### Algorithm

INPUT: Array elements

OUTPUT: the array after left rotation

PROCESS:

Step 1: [taking the input]

For i=0 to n-1 repeat

[end of ‘for’ loop]

Step 2: [Rotating the array]

Set t<-a

For i=1 to n-1 repeat

Set a[i-1]<-a[i]

[End of ‘for’ loop]

Set a[n-1]<-t

Print “The array after rotation is: “

For i=0 to n-1 repeat

Print a[i]

[End of ‘for’ loop]

Step 3: Stop.

### Code

The time complexity to take the number of elements is O(n) and the time complexity to left rotate is also O(n).

Therefore, the time complexity of this program is O(n).

t=a;--------------------------------------------------------O(1)

for(i=1;i<n;i++)---------------------------------------------O(n-1)

a[i-1]=a[i];----------------------------------------O(1)

a[n-1]=t;-----------------------------------------------------O(1)

therefore, the complexity is: O(n)