Start with an array, choose left or right rotation, shift elements accordingly, wrap around the ends, and get the rotated array.
Execution Sample
DSA Python
arr = [1, 2, 3, 4, 5]
rotate_by = 2# Left rotate by 2
rotated = arr[rotate_by:] + arr[:rotate_by]
print(rotated)
This code left rotates the array by 2 positions by slicing and concatenating.
Execution Table
Step
Operation
Array State
Explanation
Visual State
1
Start
[1, 2, 3, 4, 5]
Initial array
1 -> 2 -> 3 -> 4 -> 5 -> null
2
Choose rotation
Left by 2
Decide to rotate left by 2 positions
N/A
3
Slice from index 2 to end
[3, 4, 5]
Take elements from index 2 to end
3 -> 4 -> 5 -> null
4
Slice from start to index 2
[1, 2]
Take elements from start to index 2
1 -> 2 -> null
5
Concatenate slices
[3, 4, 5, 1, 2]
Combine slices to form rotated array
3 -> 4 -> 5 -> 1 -> 2 -> null
6
Print result
[3, 4, 5, 1, 2]
Output the rotated array
3 -> 4 -> 5 -> 1 -> 2 -> null
7
End
Rotation complete
Rotation finished
N/A
💡 Rotation done after slicing and concatenation
Variable Tracker
Variable
Start
After Step 3
After Step 4
After Step 5
Final
arr
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
rotate_by
2
2
2
2
2
rotated (temp slice 1)
N/A
[3, 4, 5]
[3, 4, 5]
[3, 4, 5]
[3, 4, 5]
rotated (temp slice 2)
N/A
N/A
[1, 2]
[1, 2]
[1, 2]
rotated (final)
N/A
N/A
N/A
[3, 4, 5, 1, 2]
[3, 4, 5, 1, 2]
Key Moments - 3 Insights
Why do we slice the array into two parts before concatenating?
Because rotating means moving elements from one end to the other. Slicing splits the array at the rotation point, so we can put the second part first and then the first part, as shown in steps 3, 4, and 5 of the execution_table.
What happens if rotate_by is 0 or equal to array length?
If rotate_by is 0 or equal to the array length, the slices will be the whole array and empty array respectively, so concatenation returns the original array unchanged. This is why the rotation stops without changing the array.
Why do we use arr[rotate_by:] + arr[:rotate_by] for left rotation?
Because arr[rotate_by:] takes elements after the rotation point, and arr[:rotate_by] takes elements before it. Putting the second slice first simulates moving the first part to the end, which is the definition of left rotation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 5, what is the array state after concatenation?
A[5, 4, 3, 2, 1]
B[1, 2, 3, 4, 5]
C[3, 4, 5, 1, 2]
D[2, 3, 4, 5, 1]
💡 Hint
Check the 'Array State' column at Step 5 in the execution_table.
At which step does the array get split into two slices?
AStep 3 and Step 4
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Operation' column where slicing happens in the execution_table.
If rotate_by was 0, what would be the final rotated array?
A[]
B[1, 2, 3, 4, 5]
C[5, 4, 3, 2, 1]
D[2, 3, 4, 5, 1]
💡 Hint
Refer to the key_moments explanation about rotate_by being 0 or equal to array length.
Concept Snapshot
Array Rotation Techniques:
- Rotate array left or right by shifting elements
- Left rotate by k: arr[k:] + arr[:k]
- Right rotate by k: arr[-k:] + arr[:-k]
- Rotation wraps elements around ends
- Useful for cyclic shifts in arrays
Full Transcript
Array rotation means moving elements in an array to the left or right by a certain number of positions. We do this by slicing the array into two parts at the rotation point and then joining them in swapped order. For example, left rotating by 2 means taking elements from index 2 to end, then from start to index 2, and joining them. This shifts the array elements left by 2 positions, wrapping the first two elements to the end. If the rotation count is zero or equal to the array length, the array stays the same. This technique is simple and efficient for rotating arrays.