Arrays · Easy

8. Cyclically Rotate an Array by One

Topics: Array

Solve it on DeVert Campus - free code runner

Problem

Given an array, cyclically rotate it to the right by one position (the last element moves to the front). Print the result.

Input and output

Input: The first line contains an integer n. The second line contains n space-separated integers. Output: Print the array after rotating it right by one position, space-separated.

Example

Input:
5
1 2 3 4 5
Output:
5 1 2 3 4

Constraints

1 <= n <= 10^5

Key observation

This is a special, simple case of the more general 'rotate an array by k positions' problem - the same shifting principle scales up, just with more elements needing to wrap around.

Intuition

Shift every element one position to the right, and wrap the very last element around to become the new first element. Brute force: Build a brand new array by copying the last element first, then the rest in order - this works, but uses O(n) extra space when the shift can be done directly within the original array. Optimised: Shifting elements in place, working from the END of the array backward toward the front, avoids overwriting any value before you've had a chance to move it - combined with remembering the original last element separately, this achieves the rotation with zero extra array needed.

Approach 1

Create a brand-new array of the same size. Place the original array's last element at index 0 of the new array, then copy the original elements at indices 0..n-2 into indices 1..n-1 of the new array. Finally copy the new array back over the original (or just return it). This is correct and simple to reason about, but it uses an entirely separate array as scratch space proportional to the input size. Time: O(n) Space: O(n)

Approach 2

Since only one element needs to move, just save the last element in a single temporary variable, then shift every other element one position to the right by walking from the end of the array toward the front (arr[i] = arr[i-1]), and finally place the saved value into index 0. This touches each element exactly once with a single pass and one temp variable, avoiding the extra reversal passes of the block-reversal method - the most direct and efficient way to rotate by exactly one position. Time: O(n) Space: O(1)

Approach 3

Use the classic block-reversal rotation trick, which generalizes to rotating by any amount k (here k=1): reverse the whole array, then reverse the first k elements, then reverse the remaining n-k elements. For a right rotation by one this becomes reverse(0, n-1) then reverse(0,0) (a no-op) then reverse(1, n-1). This performs the rotation in place with only O(1) extra space, which is already a big improvement over allocating a new array, even though for k=1 specifically it does more reversal work than strictly necessary. Time: O(n) Space: O(1)

Step by step

1. Remember the last element separately, since it's about to move to the front. 2. Shift every other element one position to the right, working from the end toward the front so you don't overwrite values you still need. 3. Place the remembered last element into the very first position.

Complexity

Time: O(n) - every element is touched exactly once during the shift. Space: O(1) - only one extra variable (the remembered last element) is needed.

Edge cases

n = 1 (rotating a single element leaves it unchanged) and n = 10^5 (largest, confirming the single-pass shift runs in O(n)).

Interview tip

Be ready to explain WHY you must shift from the back toward the front (not front to back) - shifting the wrong direction would overwrite values you still need to move, a common off-by-direction mistake.

All DSA problems