0
0
PythonProgramBeginner · 2 min read

Python Program to Rotate Array Right by N Positions

To rotate an array right by k positions in Python, use slicing like rotated = arr[-k:] + arr[:-k] where arr is your list and k is the number of rotations.
📋

Examples

Input[1, 2, 3, 4, 5], k=2
Output[4, 5, 1, 2, 3]
Input[10, 20, 30], k=1
Output[30, 10, 20]
Input[7, 8, 9], k=3
Output[7, 8, 9]
🧠

How to Think About It

To rotate an array right by k positions, think of moving the last k elements to the front while keeping the order. If k is larger than the array length, use the remainder of k divided by the length to avoid extra rotations. Use slicing to split and join the array parts.
📐

Algorithm

1
Get the input array and the number k of positions to rotate.
2
Calculate k modulo the length of the array to handle large k values.
3
Slice the array into two parts: last k elements and the rest.
4
Concatenate the last k elements at the front with the remaining elements.
5
Return or print the rotated array.
💻

Code

python
def rotate_right(arr, k):
    n = len(arr)
    k = k % n
    return arr[-k:] + arr[:-k]

# Example usage
array = [1, 2, 3, 4, 5]
rotated_array = rotate_right(array, 2)
print(rotated_array)
Output
[4, 5, 1, 2, 3]
🔍

Dry Run

Let's trace rotating [1, 2, 3, 4, 5] by 2 positions to the right.

1

Calculate length and adjust k

Length n = 5, k = 2 % 5 = 2

2

Slice last k elements

arr[-2:] = [4, 5]

3

Slice remaining elements

arr[:-2] = [1, 2, 3]

4

Concatenate slices

rotated = [4, 5] + [1, 2, 3] = [4, 5, 1, 2, 3]

StepOperationResult
1Calculate k % n2
2Slice last k elements[4, 5]
3Slice first n-k elements[1, 2, 3]
4Concatenate slices[4, 5, 1, 2, 3]
💡

Why This Works

Step 1: Adjust rotation count

Using k % n ensures the rotation count fits within the array length, avoiding unnecessary full rotations.

Step 2: Split array using slicing

Slicing arr[-k:] gets the last k elements, and arr[:-k] gets the rest, preparing for rotation.

Step 3: Combine slices to rotate

Joining the last k elements before the rest moves them to the front, achieving the right rotation.

🔄

Alternative Approaches

Using collections.deque rotate
python
from collections import deque

def rotate_right_deque(arr, k):
    d = deque(arr)
    d.rotate(k)
    return list(d)

print(rotate_right_deque([1, 2, 3, 4, 5], 2))
This method uses a built-in double-ended queue with a rotate method, which is efficient and easy to read but requires importing a module.
Rotate by repeated popping and inserting
python
def rotate_right_pop(arr, k):
    n = len(arr)
    k = k % n
    for _ in range(k):
        last = arr.pop()
        arr.insert(0, last)
    return arr

print(rotate_right_pop([1, 2, 3, 4, 5], 2))
This approach moves elements one by one and is simple to understand but less efficient for large arrays or rotations.

Complexity: O(n) time, O(n) space

Time Complexity

Slicing and concatenation each take O(n) time because they process the whole array.

Space Complexity

The rotated array is created as a new list, so space is O(n). In-place rotation would reduce space but is more complex.

Which Approach is Fastest?

Using slicing is fast and clear for most cases. The deque method is also efficient and uses optimized C code internally.

ApproachTimeSpaceBest For
SlicingO(n)O(n)Simple and readable code
collections.deque rotateO(n)O(n)Efficient built-in method
Repeated pop and insertO(k*n)O(1)Small arrays or rotations, simple logic
💡
Use modulo k % len(arr) to handle rotation counts larger than the array size.
⚠️
Forgetting to use modulo causes incorrect rotations when k is larger than the array length.