<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&family=DM+Mono:ital,wght@0,300;0,400;0,500;1,300;1,400;1,500&display=swap" rel="stylesheet"/>

Rotate Array in Dart — Reversal & Extra Array

By Mihir Pipermitwala · 2 min read

⚡ 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:

Copy
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.

Copy
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.

Copy
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!

Mihir Pipermitwala

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 →
Scroll down to load comments...
Related Blogs
Go JSON v2: A Practical Migration Guide to encoding/json/v2

Go JSON v2: A Practical Migration Guide to encoding/json/v2

Migrate a Go REST API to encoding/json/v2 in 7 steps: struct tags, omitempty, unknown fields, error handling, and jsontext streaming, plus a v1 vs v2 comparison.

GOTUTORIALDEVELOPER TOOLS

November 12, 2026

Unlocking Flutter's Potential: Best Practices for Writing Clean Code

Unlocking Flutter's Potential: Best Practices for Writing Clean Code

Unlock Flutter's potential with clean code. Learn best practices for writing maintainable Dart code, with examples and essential principles.

CLEAN CODECODE BEST PRACTICESCODE MAINTAINABILITYCODE ORGANIZATIONCODE READABILITY

June 02, 2023

Top 9 Local Databases for Flutter: Full Comparison

Top 9 Local Databases for Flutter: Full Comparison

Compare the best local databases for Flutter (Isar, Hive, Sqflite, ObjectBox, Realm, Drift, Floor, CBL, Sembast). Find pros, cons, and performance comparisons.

CODINGFLUTTERFLUTTER LOCAL DATABASESLEARN TO CODENOSQL

April 06, 2023

Related Tutorials
Essential Git Skills: How to Delete Remote Branches

Essential Git Skills: How to Delete Remote Branches

Keep your repository clean and organized. This guide walks you through how to delete both local and remote branches in Git, step by step.

BITBUCKETDELETE LOCAL BRANCHDELETE REMOTE BRANCHGITGIT BRANCH CLEANUP AUTOMATION

June 12, 2024

Show an Offline Message for No Internet in Flutter

Show an Offline Message for No Internet in Flutter

You have to use the 'Connectivity Flutter Package' to achieve this feature on your App. This package helps to know whether your device is online or offline.

CONNECTIVITY PLUSDEPENDENCIESFLUTTERFLUTTER DEVELOPMENTFLUTTER PACKAGES

April 09, 2024

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Implement TabBar and TabBarView in Flutter step by step — customize tab indicators, enable scrollable tabs, and change tabs programmatically.

CODINGDEFAULT TAB CONTROLLERFLUTTERLEARN TO CODETAB CONTROLLER

July 26, 2023

File Manipulation in Flutter: Best Practices & Examples

File Manipulation in Flutter: Best Practices & Examples

Master Flutter file manipulation — permission management, directory handling, and practical read-write scenarios to elevate your app's file handling.

CODINGFLUTTERFLUTTER DEVELOPMENTFLUTTER FILE OPERATIONFLUTTER PATH PROVIDER

June 30, 2023

Related Problems
Rotate Array in Python — Reversal & Extra Array

Rotate Array in Python — Reversal & Extra Array

Rotate an array by k steps in Python with working code. Reversal algorithm approach with O(n) time and O(1) space, plus complexity analysis.

PYTHONPROGRAMMING PATTERNSLEARN TO CODETUTORIAL

November 30, 2026

Rotate Array in Go — Reversal & Extra Array

Rotate Array in Go — Reversal & Extra Array

Rotate an array by k steps in Go with working code. Reversal algorithm approach with O(n) time and O(1) space, plus complexity analysis.

GOLANGPROGRAMMING PATTERNSLEARN TO CODETUTORIAL

November 30, 2026

Rotate Array in Swift — Reversal & Extra Array

Rotate Array in Swift — Reversal & Extra Array

Rotate an array by k steps in Swift with working code. Reversal algorithm approach with O(n) time and O(1) space, plus complexity analysis.

SWIFTPROGRAMMING PATTERNSLEARN TO CODETUTORIAL

November 30, 2026

Remove Duplicates from Sorted Array in Dart — Two Pointers & More

Remove Duplicates from Sorted Array in Dart — Two Pointers & More

Solve Remove Duplicates from Sorted Array in Dart with working code. Two Pointers (Optimal) approach with O(n) time, plus complexity analysis and interview tips.

DART PROGRAMMINGALGORITHMSCODING INTERVIEWDATA STRUCTURES

August 29, 2024

Maximum Subarray in Dart — Kadane's Algorithm (Optimal) & More

Maximum Subarray in Dart — Kadane's Algorithm (Optimal) & More

Solve Maximum Subarray in Dart with working code. Kadane's Algorithm (Optimal) approach with O(n) time, plus complexity analysis and interview tips.

DART PROGRAMMINGALGORITHMSCODING INTERVIEWDATA STRUCTURES

October 23, 2025

Related Recommended Services
Visual Studio Code for the Web

Visual Studio Code for the Web

Build with Visual Studio Code, anywhere, anytime, in your browser.

IDEVISUAL STUDIOVISUAL STUDIO CODEWEB
Renovate | Automated Dependency Updates

Renovate | Automated Dependency Updates

Renovate Bot keeps source code dependencies up-to-date using automated Pull Requests.

AUTOMATED DEPENDENCY UPDATESBUNDLERCOMPOSERGITHUBGO MODULES
Best XML Formatter and XML Beautifier

Best XML Formatter and XML Beautifier

Online XML Formatter will format xml data, helps to validate, and works as XML Converter. Save and Share XML.

XMLXML BEAUTIFIERXML CONVERTERXML FORMATXML FORMATTER
Kubecost | Kubernetes cost monitoring and management

Kubecost | Kubernetes cost monitoring and management

Kubecost started in early 2019 as an open-source tool to give developers visibility into Kubernetes spend. We maintain a deep commitment to building and supporting dedicated solutions for the open source community.

CLOUDKUBECOSTKUBERNETESOPEN SOURCESELF HOSTED
Related Recommended Stories
How GitHub reduced testing time for iOS apps with new runner features

How GitHub reduced testing time for iOS apps with new runner features

Learn how GitHub used macOS and Apple Silicon runners for GitHub Actions to build, test, and deploy our iOS app faster.

IOSGITHUBTESTINGRUNNER
5 ways to transform your workflow using GitHub Copilot and MCP

5 ways to transform your workflow using GitHub Copilot and MCP

Learn how to streamline your development workflow with five different MCP use cases.

AGENT MODECODING AGENTCOPILOTFIGMAGITHUB
One weird trick for powerful Git aliases

One weird trick for powerful Git aliases

Advanced Git Aliases

ALIASALIAS TEMPLATEATLASSIANBITBUCKETGIT
Awesome Python

Awesome Python

An opinionated list of awesome Python frameworks, libraries, software and resources

AWESOMEAWESOME PYTHONCOLLECTIONSGITHUBPYTHON
Related Recommended Tools
Find out what websites are built with - Wappalyzer

Find out what websites are built with - Wappalyzer

Find out the technology stack of any website. Create lists of websites and contacts by the technologies they use.

ADD ONSANALYTICSAPP STOREAPPLEBOOKING
Sourcetree | Free Git GUI for Mac and Windows

Sourcetree | Free Git GUI for Mac and Windows

A Git GUI that offers a visual representation of your repositories. Sourcetree is a free Git client for Windows and Mac.

GITGITHUBGITLABATLASSIANBITBUCKET
Related Recommended Videos