Check The Programming Section
To rotate an array left to right or right to left we can follow any of these two approach
Using an temporary array with size of the rotation value
By rotating item by item, without using any temporary array
Let's consider an array and rotation value (d) =2
int num[] = {3,4,5,6,7,8,9,1,2};
After the rotation of the array will be
num[] = {5,6,7,8,9,1,2,3,4};
Instead of taking any temporary array of size d we can addpot the following algorithm to rotate an array
Copy the i-th (i=0) item to a temporary variable temp = num[0];
Shift all the item of the array from right to left by one position and the array will be
num[] = {4,5,6,7,8,9,1,2,2};
Placed the temp value at the last location of the array num[size-1] = temp; Then the array will be
num[] = {4,5,6,7,8,9,1,2,3};
Increment the i by 1 and check the value of i
if it is less than d, then repeat the step - 1 to 4
else terminate
Time Complexity : O(n*d). n for swapping and d is the number of times swapping need to performed.