Topics

This technique uses a dynamic window [left, right] (often termed [tail, head]) that expands and contracts to find optimal subarrays or substrings satisfying certain conditions. Unlike sliding window with fixed window size, the size isn’t fixed.

Core Idea

  1. Expansion (head pointer): For a given tail position, the head pointer expands the window (head++) as far as possible, adding elements one by one, as long as some condition is satisfied for the window [tail, head+1]
  2. Processing: Once the head pointer cannot expand further (either reaching the end or violating the condition), the current window [tail, head] represents the largest valid window starting at tail. Process this window to update the answer (e.g., find max length, count valid windows)
  3. Contraction (tail pointer): The tail pointer moves one step forward (tail++) to explore the next potential starting position. Before the next expansion phase begins, we need to undo the “effect” of the old tail position (since it’s not part of current window)

This process repeats, iterating through all possible start positions and finding the maximal valid window for each.

Implementation

For a structured approach to implementing variable window sliding window, especially in competitive programming, consider using the snake framework. This framework provides a template with check_condition, update_state, and undo_update_state functions to systematically handle window expansion and contraction.

Classic Examples