{
  "slug": "move-zeroes",
  "title": "Move Zeroes",
  "category": "Arrays",
  "difficulty": "easy",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "Keep a write pointer for the next non-zero slot. Scan the array; each non-zero value swaps down to that slot. Zeros naturally collect at the back, order preserved.",
  "code": [
    "w = 0",
    "for i in 0 .. n-1:",
    "    if a[i] != 0: swap(w, i); w++"
  ],
  "example": {
    "array": [
      0,
      1,
      0,
      3,
      12,
      0,
      5
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 9,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Write pointer w marks the next slot for a non-zero value.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        0,
        1,
        0,
        3,
        12,
        0,
        5
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "w",
          "index": 0,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 1,
      "op": "read",
      "caption": "a[0] is 0 — skip it.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        0,
        1,
        0,
        3,
        12,
        0,
        5
      ],
      "focus": [
        "read",
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "w",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "i",
          "index": 0,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "swap",
      "caption": "1 is non-zero — move it to slot 0.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        0,
        0,
        3,
        12,
        0,
        5
      ],
      "focus": [
        "swap",
        "swap",
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "w",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "i",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "read",
      "caption": "a[2] is 0 — skip it.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        1,
        0,
        0,
        3,
        12,
        0,
        5
      ],
      "focus": [
        null,
        null,
        "read",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "w",
          "index": 1,
          "tone": "bound"
        },
        {
          "name": "i",
          "index": 2,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "swap",
      "caption": "3 is non-zero — move it to slot 1.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        0,
        0,
        12,
        0,
        5
      ],
      "focus": [
        null,
        "swap",
        null,
        "swap",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "w",
          "index": 1,
          "tone": "bound"
        },
        {
          "name": "i",
          "index": 3,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 5,
      "op": "swap",
      "caption": "12 is non-zero — move it to slot 2.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        12,
        0,
        0,
        0,
        5
      ],
      "focus": [
        null,
        null,
        "swap",
        null,
        "swap",
        null,
        null
      ],
      "markers": [
        {
          "name": "w",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "i",
          "index": 4,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 6,
      "op": "read",
      "caption": "a[5] is 0 — skip it.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        1,
        3,
        12,
        0,
        0,
        0,
        5
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        "read",
        null
      ],
      "markers": [
        {
          "name": "w",
          "index": 3,
          "tone": "bound"
        },
        {
          "name": "i",
          "index": 5,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 7,
      "op": "swap",
      "caption": "5 is non-zero — move it to slot 3.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        12,
        5,
        0,
        0,
        0
      ],
      "focus": [
        null,
        null,
        null,
        "swap",
        null,
        null,
        "swap"
      ],
      "markers": [
        {
          "name": "w",
          "index": 3,
          "tone": "bound"
        },
        {
          "name": "i",
          "index": 6,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 8,
      "op": "settle",
      "caption": "All zeros pushed to the end; order of the rest preserved.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        12,
        5,
        0,
        0,
        0
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled"
      ],
      "markers": [],
      "regions": []
    }
  ]
}