{
  "id": "arrays/sliding-window",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/arrays/sliding-window",
  "topic": {
    "slug": "arrays",
    "title": "Arrays"
  },
  "title": "Sliding Window",
  "tagline": "Reuse the last answer instead of recomputing — a window that slides, not restarts.",
  "vizKind": "array",
  "complexity": {
    "time": {
      "best": "O(n)",
      "average": "O(n)",
      "worst": "O(n)"
    },
    "space": "O(1)",
    "growth": "linear",
    "note": "Each slide is one addition and one subtraction, no matter how wide the window — so the whole sweep is O(n), not O(n·k). And it keeps only a running total and the best seen, so O(1) extra space."
  },
  "code": {
    "javascript": "function maxWindow(a, k) {\n  let sum = 0;\n  for (let i = 0; i < k; i++) sum += a[i];\n  let best = sum;\n  for (let i = k; i < a.length; i++) {\n    sum += a[i] - a[i - k];\n    best = Math.max(best, sum);\n  }\n  return best;\n}",
    "python": "def max_window(a, k):\n    total = sum(a[:k])\n    best = total\n    for i in range(k, len(a)):\n        total += a[i] - a[i - k]\n        best = max(best, total)\n\n\n    return best\n",
    "java": "int maxWindow(int[] a, int k) {\n  int sum = 0;\n  for (int i = 0; i < k; i++) sum += a[i];\n  int best = sum;\n  for (int i = k; i < a.length; i++) {\n    sum += a[i] - a[i - k];\n    best = Math.max(best, sum);\n  }\n  return best;\n}",
    "cpp": "int maxWindow(vector<int> a, int k) {\n  int sum = 0;\n  for (int i = 0; i < k; i++) sum += a[i];\n  int best = sum;\n  for (int i = k; i < a.size(); i++) {\n    sum += a[i] - a[i - k];\n    best = max(best, sum);\n  }\n  return best;\n}"
  },
  "defaultInput": [
    4,
    2,
    7,
    1,
    8,
    3,
    6,
    5
  ],
  "defaultTarget": 3,
  "inputHint": "The target sets the window size k. Find the k-in-a-row with the biggest sum.",
  "pitfalls": [
    {
      "wrong": "Recomputing the whole window sum on every step.",
      "right": "That's the slow O(n·k) version the technique exists to avoid. Add the entering value and subtract the leaving one — the rest of the window is unchanged."
    },
    {
      "wrong": "Forgetting to subtract the value that leaves the window.",
      "right": "If you only add the newcomer, the window grows instead of sliding. You must drop the value at the far-left of the old window."
    },
    {
      "wrong": "Off-by-one errors on the window's edges.",
      "right": "A window of size k starting at i covers i to i+k−1. Getting that boundary wrong is the most common bug — trace the first and last window by hand."
    }
  ],
  "quiz": [
    {
      "q": "When the window slides one step right, what changes?",
      "options": [
        "Everything — recompute from scratch",
        "One value enters on the right, one leaves on the left",
        "Only the window's size",
        "Nothing changes"
      ],
      "answer": 1,
      "why": "The new window shares all but two items with the old one. Add the entering value, subtract the leaving value, and you have the new sum — no need to re-add the middle."
    },
    {
      "q": "What does the sliding window do to an O(n·k) brute-force solution?",
      "options": [
        "Keeps it the same",
        "Reduces it to O(n)",
        "Makes it O(n²)",
        "Makes it O(log n)"
      ],
      "answer": 1,
      "why": "By updating the total in O(1) per step instead of re-summing k values, the whole pass becomes O(n) regardless of the window size k."
    },
    {
      "q": "How much extra memory does the sliding window use here?",
      "options": [
        "O(n)",
        "O(k)",
        "O(1)",
        "O(n·k)"
      ],
      "answer": 2,
      "why": "It only keeps a running total and the best sum seen so far — a couple of numbers. That's O(1), no matter how large the array or window."
    }
  ],
  "problems": [
    {
      "name": "Maximum Average Subarray I",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/maximum-average-subarray-i/"
    },
    {
      "name": "Best Time to Buy and Sell Stock",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/best-time-to-buy-and-sell-stock/"
    },
    {
      "name": "Longest Substring Without Repeating Characters",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/longest-substring-without-repeating-characters/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 9,
    "frames": [
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-2",
            "value": 2
          },
          {
            "id": "c2-7",
            "value": 7
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-8",
            "value": 8
          },
          {
            "id": "c5-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c7-5",
            "value": 5
          }
        ],
        "pointers": [
          {
            "name": "start",
            "index": 0
          },
          {
            "name": "end",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          },
          {
            "index": 1,
            "role": "active"
          },
          {
            "index": 2,
            "role": "active"
          }
        ],
        "codeLine": 3,
        "variables": {
          "k": 3,
          "windowSum": 13
        },
        "explanation": "Start with the first 3 values. Their sum is 13. This is our first window — and, for now, the best we've seen.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-2",
            "value": 2
          },
          {
            "id": "c2-7",
            "value": 7
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-8",
            "value": 8
          },
          {
            "id": "c5-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c7-5",
            "value": 5
          }
        ],
        "pointers": [
          {
            "name": "start",
            "index": 1
          },
          {
            "name": "end",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "active"
          },
          {
            "index": 2,
            "role": "active"
          },
          {
            "index": 3,
            "role": "active"
          },
          {
            "index": 0,
            "role": "discarded"
          }
        ],
        "codeLine": 6,
        "variables": {
          "entering": 1,
          "leaving": 4,
          "windowSum": 10,
          "best": 13
        },
        "explanation": "Slide the window right by one: add 1 coming in, subtract 4 going out. New sum: 10. We didn't re-add the middle values — that's the saving.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-2",
            "value": 2
          },
          {
            "id": "c2-7",
            "value": 7
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-8",
            "value": 8
          },
          {
            "id": "c5-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c7-5",
            "value": 5
          }
        ],
        "pointers": [
          {
            "name": "start",
            "index": 2
          },
          {
            "name": "end",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "active"
          },
          {
            "index": 3,
            "role": "active"
          },
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 1,
            "role": "discarded"
          }
        ],
        "codeLine": 6,
        "variables": {
          "entering": 8,
          "leaving": 2,
          "windowSum": 16,
          "best": 13
        },
        "explanation": "Slide the window right by one: add 8 coming in, subtract 2 going out. New sum: 16. We didn't re-add the middle values — that's the saving.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-2",
            "value": 2
          },
          {
            "id": "c2-7",
            "value": 7
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-8",
            "value": 8
          },
          {
            "id": "c5-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c7-5",
            "value": 5
          }
        ],
        "pointers": [
          {
            "name": "start",
            "index": 2
          },
          {
            "name": "end",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 7,
        "variables": {
          "best": 16,
          "windowSum": 16
        },
        "explanation": "16 beats the old best. This is the new best window.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-2",
            "value": 2
          },
          {
            "id": "c2-7",
            "value": 7
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-8",
            "value": 8
          },
          {
            "id": "c5-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c7-5",
            "value": 5
          }
        ],
        "pointers": [
          {
            "name": "start",
            "index": 3
          },
          {
            "name": "end",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "active"
          },
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 5,
            "role": "active"
          },
          {
            "index": 2,
            "role": "discarded"
          }
        ],
        "codeLine": 6,
        "variables": {
          "entering": 3,
          "leaving": 7,
          "windowSum": 12,
          "best": 16
        },
        "explanation": "Slide the window right by one: add 3 coming in, subtract 7 going out. New sum: 12. We didn't re-add the middle values — that's the saving.",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-2",
            "value": 2
          },
          {
            "id": "c2-7",
            "value": 7
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-8",
            "value": 8
          },
          {
            "id": "c5-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c7-5",
            "value": 5
          }
        ],
        "pointers": [
          {
            "name": "start",
            "index": 4
          },
          {
            "name": "end",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 5,
            "role": "active"
          },
          {
            "index": 6,
            "role": "active"
          },
          {
            "index": 3,
            "role": "discarded"
          }
        ],
        "codeLine": 6,
        "variables": {
          "entering": 6,
          "leaving": 1,
          "windowSum": 17,
          "best": 16
        },
        "explanation": "Slide the window right by one: add 6 coming in, subtract 1 going out. New sum: 17. We didn't re-add the middle values — that's the saving.",
        "stats": {
          "comparisons": 4,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-2",
            "value": 2
          },
          {
            "id": "c2-7",
            "value": 7
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-8",
            "value": 8
          },
          {
            "id": "c5-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c7-5",
            "value": 5
          }
        ],
        "pointers": [
          {
            "name": "start",
            "index": 4
          },
          {
            "name": "end",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 7,
        "variables": {
          "best": 17,
          "windowSum": 17
        },
        "explanation": "17 beats the old best. This is the new best window.",
        "stats": {
          "comparisons": 4,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-2",
            "value": 2
          },
          {
            "id": "c2-7",
            "value": 7
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-8",
            "value": 8
          },
          {
            "id": "c5-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c7-5",
            "value": 5
          }
        ],
        "pointers": [
          {
            "name": "start",
            "index": 5
          },
          {
            "name": "end",
            "index": 7
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "active"
          },
          {
            "index": 6,
            "role": "active"
          },
          {
            "index": 7,
            "role": "active"
          },
          {
            "index": 4,
            "role": "discarded"
          }
        ],
        "codeLine": 6,
        "variables": {
          "entering": 5,
          "leaving": 8,
          "windowSum": 14,
          "best": 17
        },
        "explanation": "Slide the window right by one: add 5 coming in, subtract 8 going out. New sum: 14. We didn't re-add the middle values — that's the saving.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-2",
            "value": 2
          },
          {
            "id": "c2-7",
            "value": 7
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-8",
            "value": 8
          },
          {
            "id": "c5-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c7-5",
            "value": 5
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 9,
        "variables": {
          "answer": 17,
          "slides": 5
        },
        "explanation": "The best window sums to 17. We slid across the whole array touching each value about twice — O(n) — instead of re-summing every window from scratch.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        },
        "accent": "sorted"
      }
    ]
  }
}