{
  "slug": "rotate-array",
  "title": "Rotate an Array",
  "category": "Arrays",
  "difficulty": "medium",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "To left-rotate by k, reverse the first k elements, reverse the rest, then reverse the whole array. Three in-place reversals rotate with no extra memory.",
  "code": [
    "rotateLeft(k):",
    "    reverse(0, k-1)",
    "    reverse(k, n-1)",
    "    reverse(0, n-1)"
  ],
  "example": {
    "array": [
      1,
      2,
      3,
      4,
      5,
      6,
      7
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 11,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Left-rotate by k = 3 using three reversals.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        1,
        2,
        3,
        4,
        5,
        6,
        7
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "say",
      "caption": "Reverse the first 3 elements.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        1,
        2,
        3,
        4,
        5,
        6,
        7
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "rev",
          "from": 0,
          "to": 2,
          "tone": "window"
        }
      ]
    },
    {
      "i": 2,
      "op": "swap",
      "caption": "Swap 1 and 3.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        2,
        1,
        4,
        5,
        6,
        7
      ],
      "focus": [
        "swap",
        null,
        "swap",
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "rev",
          "from": 0,
          "to": 2,
          "tone": "window"
        }
      ]
    },
    {
      "i": 3,
      "op": "say",
      "caption": "Reverse the remaining 4.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        3,
        2,
        1,
        4,
        5,
        6,
        7
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "rev",
          "from": 3,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 4,
      "op": "swap",
      "caption": "Swap 4 and 7.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        3,
        2,
        1,
        7,
        5,
        6,
        4
      ],
      "focus": [
        null,
        null,
        null,
        "swap",
        null,
        null,
        "swap"
      ],
      "markers": [],
      "regions": [
        {
          "label": "rev",
          "from": 3,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 5,
      "op": "swap",
      "caption": "Swap 5 and 6.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        3,
        2,
        1,
        7,
        6,
        5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "swap",
        "swap",
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "rev",
          "from": 3,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 6,
      "op": "say",
      "caption": "Reverse the whole array — now left-rotated by 3.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        3,
        2,
        1,
        7,
        6,
        5,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "rev",
          "from": 0,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 7,
      "op": "swap",
      "caption": "Swap 3 and 4.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        4,
        2,
        1,
        7,
        6,
        5,
        3
      ],
      "focus": [
        "swap",
        null,
        null,
        null,
        null,
        null,
        "swap"
      ],
      "markers": [],
      "regions": [
        {
          "label": "rev",
          "from": 0,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 8,
      "op": "swap",
      "caption": "Swap 2 and 5.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        4,
        5,
        1,
        7,
        6,
        2,
        3
      ],
      "focus": [
        null,
        "swap",
        null,
        null,
        null,
        "swap",
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "rev",
          "from": 0,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 9,
      "op": "swap",
      "caption": "Swap 1 and 6.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        4,
        5,
        6,
        7,
        1,
        2,
        3
      ],
      "focus": [
        null,
        null,
        "swap",
        null,
        "swap",
        null,
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "rev",
          "from": 0,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 10,
      "op": "settle",
      "caption": "Rotated left by 3.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        4,
        5,
        6,
        7,
        1,
        2,
        3
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled"
      ],
      "markers": [],
      "regions": []
    }
  ]
}