A specific application of Two Pointers. Sliding Window Technique is a method used to solve problems that involve subarray or substring or window.

  • Using 2 points l, r to maintain state some state and move across an array. l and r can only move in one direction, adding or subtracting to and from the window.
  • Instead of repeatedly iterating over the same elements, the sliding window maintains a range (or “window”) that moves step-by-step through the data, updating results incrementally.
  • The main idea is to use the results of previous window to do computations for the next window.
  • Commonly used for problems like finding subarrays with a specific sum, finding the longest substring with unique characters, or solving problems that require a fixed-size window to process elements efficiently.

Allows you to solve many problems in O(n) time.

Use with Kadane’s Algorithm to solve Maximum Subarray Sum.