Given an integer array arr, rotate it by one position in the clockwise direction and return the result.
A clockwise rotation by one takes the last element and moves it to the front; every other element shifts one slot to the right. So arr = [1, 2, 3, 4, 5] becomes [5, 1, 2, 3, 4].
The array always has at least one element. A single-element array is unchanged by rotation.
Input: arr = [1, 2, 3, 4, 5] Output: [5, 1, 2, 3, 4] The last element 5 moves to the front and the rest shift right by one.
Input: arr = [9, 8, 7, 6, 4, 2, 1, 3] Output: [3, 9, 8, 7, 6, 4, 2, 1] The last element 3 comes to the first position; everything else slides one slot right.
- 1 <= arr.size <= 10^5 - 0 <= arr[i] <= 10^5
Rotating an array by one looks trivial, and it is — but it's the cleanest place to learn a habit that saves you on every in-place array problem: the direction you sweep decides whether you corrupt your data. The brute force copies into a fresh array; the optimal does the same shift in place with a single saved variable.
O(n) space) and a careful in-place shift (O(1)).Given an array arr, rotate it clockwise by one: the last element moves to index 0, and every other element shifts one slot to the right. A single-element array is unchanged.
Worked example — arr = [1, 2, 3, 4, 5]
before: 1 2 3 4 5
└──────┐ (last element wraps to front)
after: 5 1 2 3 4 ✓
“Clockwise or counter-clockwise?”
Clockwise here: the last element wraps to the front. Counter-clockwise (a left rotation) would send the first element to the back.
“Is the array guaranteed non-empty?”
Yes (at least one element); a single-element array is unchanged.
“What about a one-element array?”
Rotation leaves it exactly as it was — a useful check that your loop bounds don't misbehave.
“Return a new array, or mutate the input in place?”
Either is accepted. That choice is exactly the difference between the two approaches — a copy (O(n) space) or an in-place shift (O(1)).
“How large can the array be?”
Up to 10^5. Both approaches are O(n) time; the only axis to optimise is the extra space.
Two quick questions.
Is this a clockwise rotation — the last element moves to the front?
Am I allowed to mutate the input in place, or should I return a fresh array?
If in-place matters, I'll do it in O(1) extra space by shifting carefully.
A rotation by one isn't a big reshuffle: the last element jumps to the front, and every other element moves exactly one step. Seeing that "only the last one is special" is what makes an in-place solution obvious.
Save the last element aside, slide everyone one slot to the right, then drop the saved value into index 0. A single temporary replaces an entire O(n) copy.
This is the transferable lesson. If you shift front-to-back, you overwrite arr[i] before the next step gets to read it — corrupting the array. Sweeping from the last index down to 1 (arr[i] = arr[i-1]) always reads a cell before it's overwritten.
| Copy into fresh array | In-place shift | |
|---|---|---|
| Idea | Seed with last, copy the rest | Save last, slide right, drop at front |
| Time | O(n) | O(n) |
| Space | O(n) | O(1) |
| Mutates input? |
The full code for both is in the Approaches selector below.
Key takeaway
Rotating by one is a one-slot shift with a single wrap-around, doable in O(1) space with one temporary. The real takeaway is the sweep direction: when you shift elements in place, move so that every source cell is read before it's overwritten — here, back-to-front.