Array Rotation Techniques in DSA Python - Time & Space Complexity
When we rotate an array, we move elements around to new positions. Understanding how long this takes helps us choose the best method.
We want to know how the time needed grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
def rotate_array(arr, k):
n = len(arr)
k = k % n
rotated = arr[-k:] + arr[:-k]
return rotated
This code rotates the array to the right by k steps by slicing and joining parts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating new slices of the array and concatenating them.
- How many times: Each element is accessed once during slicing and once during concatenation, so about 2n operations.
As the array size grows, the number of operations grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 (2 x 10) |
| 100 | About 200 (2 x 100) |
| 1000 | About 2000 (2 x 1000) |
Pattern observation: The operations grow linearly with the size of the array.
Time Complexity: O(n)
This means the time to rotate grows directly with the number of elements in the array.
[X] Wrong: "Rotating an array by k steps takes constant time because we just move elements."
[OK] Correct: Even though we move elements logically, the computer must access and copy each element, so time grows with array size.
Understanding how array rotation works and its time cost helps you explain your choices clearly and shows you know how to handle data efficiently.
"What if we rotated the array in place without creating a new array? How would the time complexity change?"
Practice
Solution
Step 1: Understand left rotation
Left rotation by 1 means the element at index 0 moves to the end, and all others shift left by one.Step 2: Visualize the movement
For example, [1, 2, 3] becomes [2, 3, 1] after left rotation by 1.Final Answer:
Moves each element one step to the left, first element goes to the end -> Option BQuick Check:
Left rotation = shift left + wrap first element [OK]
- Confusing left rotation with right rotation
- Thinking rotation sorts the array
- Assuming rotation reverses the array
arr to the right by 2 positions using slicing?Solution
Step 1: Understand right rotation with slicing
Right rotation by 2 means last 2 elements move to front, rest shift right.Step 2: Apply slicing syntax
arr[-2:] gets last 2 elements, arr[:-2] gets all except last 2. Concatenating gives rotated array.Final Answer:
arr = arr[-2:] + arr[:-2] -> Option AQuick Check:
Right rotation by 2 = last 2 + rest [OK]
- Using + operator incorrectly with slices
- Confusing left and right rotation slices
- Using subtraction operator instead of concatenation
arr = [10, 20, 30, 40, 50] k = 3 rotated = arr[k:] + arr[:k] print(rotated)
Solution
Step 1: Identify rotation type
arr[k:] + arr[:k] rotates array left by k=3 positions.Step 2: Perform slicing and concatenation
arr[3:] = [40, 50], arr[:3] = [10, 20, 30], combined = [40, 50, 10, 20, 30]Final Answer:
[40, 50, 10, 20, 30] -> Option DQuick Check:
Left rotation by 3 = [40,50] + [10,20,30] [OK]
- Mixing left and right rotation logic
- Incorrect slicing indices
- Misreading k value
arr to the right by k positions. What is the error?def rotate_right(arr, k):
n = len(arr)
k = k % n
rotated = arr[k:] + arr[:k]
return rotated
print(rotate_right([1,2,3,4,5], 2))Solution
Step 1: Analyze slicing for rotation
arr[k:] + arr[:k] rotates array left by k, not right.Step 2: Correct right rotation slicing
Right rotation by k should be arr[-k:] + arr[:-k].Final Answer:
The slicing indices perform left rotation, not right rotation -> Option AQuick Check:
Right rotation needs arr[-k:] + arr[:-k] [OK]
- Using left rotation slices for right rotation
- Ignoring modulo to handle k > n
- Forgetting to return the rotated array
arr = [3, 8, 9, 7, 6], how would you rotate it to the right by k = 3 positions using a single Python statement without slicing?Solution
Step 1: Understand rotation without slicing
Rotating right by 3 means moving last element to front 3 times.Step 2: Use loop with pop and insert
Loop 3 times: pop last element and insert at index 0 to rotate right.Final Answer:
Use a loop to pop last element and insert at front 3 times -> Option CQuick Check:
Pop + insert at front repeated k times = right rotation [OK]
- Using reverse instead of rotation
- Sorting array mistakenly
- Only popping once instead of k times
Ready to prove it?
Pass the test and this pattern moves unknown
