Python Program for Array Rotation Using temp array

Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. 

 

Rotation of the above array by 2 will make array

 

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]

Python3




# function to rotate array by d elements using temp array
def rotateArray(arr, n, d):
    temp = []
    i = 0
    while (i < d):
        temp.append(arr[i])
        i = i + 1
    i = 0
    while (d < n):
        arr[i] = arr[d]
        i = i + 1
        d = d + 1
    arr[:] = arr[: i] + temp
    return arr
 
 
# Driver function to test above function
arr = [1, 2, 3, 4, 5, 6, 7]
print("Array after left rotation is: ", end=' ')
print(rotateArray(arr, len(arr), 2))


Output

Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]

Time complexity: O(n) 
Auxiliary Space: O(d)

Python Program for Array Rotation

Here we are going to see how we can rotate array with Python code.

Array Rotation:

 

Similar Reads

Python Program for Array Rotation Example

Partitioning the sub arrays and reversing them...

Python Program for Array Rotation Using temp array

...

Python Program for Array Rotation Using Rotate one by one

Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements....