{
  "slug": "prefix-sum",
  "title": "Prefix Sum",
  "category": "Arrays",
  "difficulty": "easy",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "Each position becomes the sum of everything up to and including it: a[i] += a[i-1]. Afterwards any range sum is a single subtraction.",
  "code": [
    "for i in 1 .. n-1:",
    "    a[i] = a[i] + a[i-1]"
  ],
  "example": {
    "array": [
      3,
      1,
      4,
      1,
      5,
      9,
      2
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 14,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Build running totals in place.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        3,
        1,
        4,
        1,
        5,
        9,
        2
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "compare",
      "caption": "a[1] = a[1] + a[0] = 1 + 3.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        3,
        1,
        4,
        1,
        5,
        9,
        2
      ],
      "focus": [
        "compare",
        "compare",
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "write",
      "caption": "Now a[1] = 4.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        4,
        1,
        5,
        9,
        2
      ],
      "focus": [
        null,
        "write",
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "compare",
      "caption": "a[2] = a[2] + a[1] = 4 + 4.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        4,
        1,
        5,
        9,
        2
      ],
      "focus": [
        null,
        "compare",
        "compare",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 2,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "write",
      "caption": "Now a[2] = 8.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        8,
        1,
        5,
        9,
        2
      ],
      "focus": [
        null,
        null,
        "write",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 2,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 5,
      "op": "compare",
      "caption": "a[3] = a[3] + a[2] = 1 + 8.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        8,
        1,
        5,
        9,
        2
      ],
      "focus": [
        null,
        null,
        "compare",
        "compare",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 6,
      "op": "write",
      "caption": "Now a[3] = 9.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        8,
        9,
        5,
        9,
        2
      ],
      "focus": [
        null,
        null,
        null,
        "write",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 7,
      "op": "compare",
      "caption": "a[4] = a[4] + a[3] = 5 + 9.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        8,
        9,
        5,
        9,
        2
      ],
      "focus": [
        null,
        null,
        null,
        "compare",
        "compare",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 8,
      "op": "write",
      "caption": "Now a[4] = 14.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        8,
        9,
        14,
        9,
        2
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "write",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 9,
      "op": "compare",
      "caption": "a[5] = a[5] + a[4] = 9 + 14.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        8,
        9,
        14,
        9,
        2
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "compare",
        "compare",
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 10,
      "op": "write",
      "caption": "Now a[5] = 23.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        8,
        9,
        14,
        23,
        2
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        "write",
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 11,
      "op": "compare",
      "caption": "a[6] = a[6] + a[5] = 2 + 23.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        8,
        9,
        14,
        23,
        2
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        "compare",
        "compare"
      ],
      "markers": [
        {
          "name": "i",
          "index": 6,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 12,
      "op": "write",
      "caption": "Now a[6] = 25.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        8,
        9,
        14,
        23,
        25
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        "write"
      ],
      "markers": [
        {
          "name": "i",
          "index": 6,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 13,
      "op": "settle",
      "caption": "Each cell now holds the sum of everything up to it.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        4,
        8,
        9,
        14,
        23,
        25
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled"
      ],
      "markers": [],
      "regions": []
    }
  ]
}