⚡ TL;DR
Rotate an array by k steps in Dart with working code. Reversal algorithm approach with O(n) time and O(1) space, plus complexity analysis.
Rotate Array (#189 — Medium): Rotate the array to the right by k steps, in-place, where k is non-negative.
Problem Statement
Rotate the array to the right by k steps, in-place, where k is non-negative.
Example:
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation: each element moves 3 positions to the right, wrapping around.Approach: Reversal Algorithm
Time: O(n) | Space: O(1)
Reverse the whole array, then reverse the first k and the remaining n−k elements separately. Three in-place reversals produce the rotation with no extra buffer.
void rotateArray(List<int> nums, int k) {
void reverse(int lo, int hi) {
while (lo < hi) {
int tmp = nums[lo];
nums[lo] = nums[hi];
nums[hi] = tmp;
lo++;
hi--;
}
}
k %= nums.length;
reverse(0, nums.length - 1);
reverse(0, k - 1);
reverse(k, nums.length - 1);
}
void main() {
List<int> nums = [1, 2, 3, 4, 5, 6, 7];
rotateArray(nums, 3);
print('Rotated: [${nums.join(", ")}]');
}Approach: Extra Array
Time: O(n) | Space: O(n)
Slice the last k elements to the front and the rest behind them, then copy back. The clearest to read — at the cost of O(n) auxiliary space.
void rotateArrayExtra(List<int> nums, int k) {
int n = nums.length;
k %= n;
List<int> rotated = [...nums.sublist(n - k), ...nums.sublist(0, n - k)];
for (int i = 0; i < n; i++) {
nums[i] = rotated[i];
}
}
void main() {
List<int> nums = [1, 2, 3, 4, 5, 6, 7];
rotateArrayExtra(nums, 3);
print('Rotated: [${nums.join(", ")}]');
}Key Takeaways
- k %= n first — rotating by n or more wraps back to the same array
- The reversal trick (reverse all, reverse both halves) is the standard in-place rotation
- The extra-array version is simpler and perfectly fine when memory is not constrained
FAQ
What is the best approach to solve Rotate Array in Dart?
The recommended approach is Reversal Algorithm, which runs in O(n) time with O(1) space. The full Dart implementation is shown above.
What is the time complexity of Rotate Array in Dart?
Using Reversal Algorithm, the time complexity is O(n) and the space complexity is O(1); Extra Array runs in O(n) time with O(n) space.
Happy coding!

Written by Mihir Pipermitwala
Senior Full Stack Consultant & Mobile Engineering Specialist building user-focused applications in healthcare with Flutter, Dart and React Native. I write about what I actually build and test — every tutorial here comes from real project work.
More about Mihir →