0
0
DSA Pythonprogramming

Array Rotation Techniques in DSA Python

Choose your learning style9 modes available
Mental Model
Array rotation means moving elements so the array looks shifted left or right by some steps. We imagine the array as a circle where elements wrap around.
Analogy: Think of a carousel with horses arranged in a circle. Rotating the carousel moves each horse forward or backward, but the order stays the same, just shifted.
Initial array:
[1] -> [2] -> [3] -> [4] -> [5] -> null
Rotate by 2 steps to the left means:
[3] -> [4] -> [5] -> [1] -> [2] -> null
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], rotate left by 2 steps
Goal: Shift the array elements left by 2 positions so the first two elements move to the end
Step 1: Reverse the first 2 elements
[2, 1, 3, 4, 5]
Why: Reversing the first part prepares for rotation by breaking the array into parts
Step 2: Reverse the remaining elements from index 2 to end
[2, 1, 5, 4, 3]
Why: Reversing the second part isolates the elements that will move to the front
Step 3: Reverse the whole array
[3, 4, 5, 1, 2]
Why: Reversing the entire array completes the rotation by putting elements in correct order
Result:
[3, 4, 5, 1, 2]
Annotated Code
DSA Python
def reverse(arr, start, end):
    while start < end:
        arr[start], arr[end] = arr[end], arr[start]
        start += 1
        end -= 1

def rotate_left(arr, k):
    n = len(arr)
    k %= n  # handle rotation greater than length
    reverse(arr, 0, k - 1)  # reverse first k elements
    reverse(arr, k, n - 1)  # reverse rest
    reverse(arr, 0, n - 1)  # reverse whole array

arr = [1, 2, 3, 4, 5]
rotate_left(arr, 2)
print(arr)
k %= n # handle rotation greater than length
adjust rotation steps to within array length
reverse(arr, 0, k - 1) # reverse first k elements
reverse the first part to prepare for rotation
reverse(arr, k, n - 1) # reverse rest
reverse the remaining part to isolate elements
reverse(arr, 0, n - 1) # reverse whole array
reverse entire array to complete rotation
OutputSuccess
[3, 4, 5, 1, 2]
Complexity Analysis
Time: O(n) because each element is reversed at most twice during the three reversals
Space: O(1) because rotation is done in place without extra arrays
vs Alternative: Naive approach shifts elements one by one in O(n*k) time, which is slower for large k; reversal method is efficient and uses constant space
Edge Cases
rotation steps k is 0
array remains unchanged
DSA Python
k %= n  # handle rotation greater than length
rotation steps k equal to array length
array remains unchanged because full rotation returns to original
DSA Python
k %= n  # handle rotation greater than length
empty array
no operation, array stays empty
DSA Python
n = len(arr)
When to Use This Pattern
When you need to shift array elements efficiently without extra space, use the reversal technique because it rotates in linear time with constant space.
Common Mistakes
Mistake: Rotating by k without taking modulo with array length causes index errors or incorrect results
Fix: Always use k %= n to adjust rotation steps within array length
Mistake: Trying to rotate by moving elements one by one leads to slow O(n*k) time
Fix: Use the reversal method to achieve O(n) time
Summary
Rotates array elements left or right by k steps efficiently using reversals.
Use when you want to shift array elements without extra space and in linear time.
The key insight is that reversing parts of the array and then the whole array achieves rotation.