{
  "slug": "array-reverse",
  "title": "Reverse an Array",
  "category": "Arrays",
  "difficulty": "easy",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "Swap the first and last, then the second and second-last, closing in until the pointers meet. In-place, no extra memory.",
  "code": [
    "lo, hi = 0, n-1",
    "while lo < hi:",
    "    swap a[lo], a[hi]",
    "    lo += 1; hi -= 1"
  ],
  "example": {
    "array": [
      3,
      8,
      1,
      6,
      9,
      4,
      7
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 10,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Reverse in place: two pointers start at the ends and move toward each other.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        3,
        8,
        1,
        6,
        9,
        4,
        7
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "compare",
      "caption": "Trade the ends: index 0 (3) with index 6 (7).",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        8,
        1,
        6,
        9,
        4,
        7
      ],
      "focus": [
        "compare",
        null,
        null,
        null,
        null,
        null,
        "compare"
      ],
      "markers": [
        {
          "name": "lo",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 6,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "swap",
      "caption": "Swap 3 and 7.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        7,
        8,
        1,
        6,
        9,
        4,
        3
      ],
      "focus": [
        "swap",
        null,
        null,
        null,
        null,
        null,
        "swap"
      ],
      "markers": [
        {
          "name": "lo",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 6,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "say",
      "caption": "Step inward — lo = 1, hi = 5.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        7,
        8,
        1,
        6,
        9,
        4,
        3
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "lo",
          "index": 1,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "compare",
      "caption": "Trade the ends: index 1 (8) with index 5 (4).",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        7,
        8,
        1,
        6,
        9,
        4,
        3
      ],
      "focus": [
        null,
        "compare",
        null,
        null,
        null,
        "compare",
        null
      ],
      "markers": [
        {
          "name": "lo",
          "index": 1,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 5,
      "op": "swap",
      "caption": "Swap 8 and 4.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        7,
        4,
        1,
        6,
        9,
        8,
        3
      ],
      "focus": [
        null,
        "swap",
        null,
        null,
        null,
        "swap",
        null
      ],
      "markers": [
        {
          "name": "lo",
          "index": 1,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 6,
      "op": "say",
      "caption": "Step inward — lo = 2, hi = 4.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        7,
        4,
        1,
        6,
        9,
        8,
        3
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "lo",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 7,
      "op": "compare",
      "caption": "Trade the ends: index 2 (1) with index 4 (9).",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        7,
        4,
        1,
        6,
        9,
        8,
        3
      ],
      "focus": [
        null,
        null,
        "compare",
        null,
        "compare",
        null,
        null
      ],
      "markers": [
        {
          "name": "lo",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 8,
      "op": "swap",
      "caption": "Swap 1 and 9.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        7,
        4,
        9,
        6,
        1,
        8,
        3
      ],
      "focus": [
        null,
        null,
        "swap",
        null,
        "swap",
        null,
        null
      ],
      "markers": [
        {
          "name": "lo",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "hi",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 9,
      "op": "settle",
      "caption": "Reversed — the order is flipped end to end.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        7,
        4,
        9,
        6,
        1,
        8,
        3
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled"
      ],
      "markers": [],
      "regions": []
    }
  ]
}