Left Rotation on an Array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A left rotation moves the first part of an array to the end while preserving order. For example, rotating [1, 2, 3, 4, 5] left by 2 gives [3, 4, 5, 1, 2].
This is a common interview problem because it tests indexing, modular arithmetic, and awareness of time and space tradeoffs.
Normalize the Rotation Count
If the array has length n, rotating left by k is the same as rotating by k % n. That matters because rotating by the full length should leave the array unchanged.
A clean Python version looks like this:
This prints:
Why Slicing Works
After normalization, the array can be split into two parts:
- the suffix starting at index
k - the prefix before index
k
Concatenating them in that order produces the rotated result. This approach is simple and correct, which makes it a great default in high-level languages.
In-Place Rotation with Reversal
If you need an in-place algorithm instead of building a new array, the classic solution is three reversals:
- reverse the first
kelements - reverse the remaining
n - kelements - reverse the whole array
Here is a Python implementation:
This runs in O(n) time and uses O(1) extra space.
Use Modular Indexing for a Read-Only View
Sometimes you do not need to physically rotate the array at all. You only need to read elements as if it had been rotated. In that case, modular indexing is enough:
This is useful in ring buffers and queue-like data structures where moving elements would be wasteful.
The same idea is important in performance-sensitive code. If rotation is only a logical view, changing an offset variable is often cheaper than shuffling the whole array in memory.
That is why many circular-buffer implementations do not rotate storage at all. They rotate the interpretation of the storage.
The data stays still; the starting index moves.
Choose the Right Approach
The best method depends on the requirement:
- use slicing when clarity matters most
- use the reversal method when you must rotate in place
- use modular indexing when you only need a rotated view
All three ideas are valid. The important part is matching the method to the actual constraint.
Common Pitfalls
- Forgetting to reduce
kwith modulo before rotating. - Mishandling the empty-array case and dividing by zero in
k % n. - Writing an in-place algorithm when a simple slice would have been clearer.
- Confusing left rotation with right rotation and moving elements in the wrong direction.
- Rebuilding the whole array repeatedly inside a loop when one normalized rotation would do.
Summary
- A left rotation by
kmoves the firstkelements to the end of the array. - Normalize the rotation count with
k % len(array). - Slicing is the simplest high-level implementation.
- The reversal algorithm gives an in-place
O(n)solution withO(1)extra space. - Sometimes modular indexing is enough and no physical rotation is needed.
Related reading
- Lempel-Ziv-Welch decompression non-existent index
- Length of longest subarray of sum less than or equal to k
- Levenshtein Distance Algorithm better than Onm?
- Levenshtein distance how to better handle words swapping positions?
- Library for working with potentially infinite graphs defined by neighbor-list functions
- Lime vs TreeInterpreter for interpreting decision tree
- Levenshtein Distance Inferring the edit operations from the matrix
- Levenshtein Matrix using only a diagonal strip

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.