{
  "slug": "sliding-window",
  "title": "Sliding Window (Max Sum)",
  "category": "Arrays",
  "difficulty": "easy",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "For a window of size k, sum the first window, then slide: add the entering element and subtract the leaving one. No value is ever re-summed.",
  "code": [
    "sum = sum of first k",
    "best = sum",
    "for i in k .. n-1:",
    "    sum += a[i] - a[i-k]",
    "    best = max(best, sum)"
  ],
  "example": {
    "array": [
      2,
      1,
      5,
      1,
      3,
      2,
      8,
      1
    ]
  },
  "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": "read",
      "caption": "First window [0..2] sums to 8.",
      "note": null,
      "codeLine": 1,
      "stats": {
        "k": 3,
        "sum": 8,
        "best": 8
      },
      "array": [
        2,
        1,
        5,
        1,
        3,
        2,
        8,
        1
      ],
      "focus": [
        "read",
        "read",
        "read",
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "win",
          "from": 0,
          "to": 2,
          "tone": "window"
        }
      ]
    },
    {
      "i": 1,
      "op": "say",
      "caption": "Slide: +1 enters, -2 leaves. Window sum = 7.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "k": 3,
        "sum": 7,
        "best": 8
      },
      "array": [
        2,
        1,
        5,
        1,
        3,
        2,
        8,
        1
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "in",
          "index": 3,
          "tone": "compare"
        },
        {
          "name": "out",
          "index": 0,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "win",
          "from": 1,
          "to": 3,
          "tone": "window"
        }
      ]
    },
    {
      "i": 2,
      "op": "say",
      "caption": "Slide: +3 enters, -1 leaves. Window sum = 9.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "k": 3,
        "sum": 9,
        "best": 8
      },
      "array": [
        2,
        1,
        5,
        1,
        3,
        2,
        8,
        1
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "in",
          "index": 4,
          "tone": "compare"
        },
        {
          "name": "out",
          "index": 1,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "win",
          "from": 2,
          "to": 4,
          "tone": "window"
        }
      ]
    },
    {
      "i": 3,
      "op": "say",
      "caption": "New best window sum 9 over indices 2..4.",
      "note": null,
      "codeLine": 5,
      "stats": {
        "k": 3,
        "sum": 9,
        "best": 9
      },
      "array": [
        2,
        1,
        5,
        1,
        3,
        2,
        8,
        1
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "in",
          "index": 4,
          "tone": "compare"
        },
        {
          "name": "out",
          "index": 1,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "win",
          "from": 2,
          "to": 4,
          "tone": "window"
        }
      ]
    },
    {
      "i": 4,
      "op": "say",
      "caption": "Slide: +2 enters, -5 leaves. Window sum = 6.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "k": 3,
        "sum": 6,
        "best": 9
      },
      "array": [
        2,
        1,
        5,
        1,
        3,
        2,
        8,
        1
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "in",
          "index": 5,
          "tone": "compare"
        },
        {
          "name": "out",
          "index": 2,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "win",
          "from": 3,
          "to": 5,
          "tone": "window"
        }
      ]
    },
    {
      "i": 5,
      "op": "say",
      "caption": "Slide: +8 enters, -1 leaves. Window sum = 13.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "k": 3,
        "sum": 13,
        "best": 9
      },
      "array": [
        2,
        1,
        5,
        1,
        3,
        2,
        8,
        1
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "in",
          "index": 6,
          "tone": "compare"
        },
        {
          "name": "out",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "win",
          "from": 4,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 6,
      "op": "say",
      "caption": "New best window sum 13 over indices 4..6.",
      "note": null,
      "codeLine": 5,
      "stats": {
        "k": 3,
        "sum": 13,
        "best": 13
      },
      "array": [
        2,
        1,
        5,
        1,
        3,
        2,
        8,
        1
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "in",
          "index": 6,
          "tone": "compare"
        },
        {
          "name": "out",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "win",
          "from": 4,
          "to": 6,
          "tone": "window"
        }
      ]
    },
    {
      "i": 7,
      "op": "say",
      "caption": "Slide: +1 enters, -3 leaves. Window sum = 11.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "k": 3,
        "sum": 11,
        "best": 13
      },
      "array": [
        2,
        1,
        5,
        1,
        3,
        2,
        8,
        1
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "in",
          "index": 7,
          "tone": "compare"
        },
        {
          "name": "out",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "win",
          "from": 5,
          "to": 7,
          "tone": "window"
        }
      ]
    },
    {
      "i": 8,
      "op": "settle",
      "caption": "Best window: sum 13 over indices 4..6.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "k": 3,
        "sum": 11,
        "best": 13
      },
      "array": [
        2,
        1,
        5,
        1,
        3,
        2,
        8,
        1
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "settled",
        "settled",
        "settled",
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "win",
          "from": 4,
          "to": 6,
          "tone": "window"
        }
      ]
    }
  ]
}