Two-Pointer &
Sliding Window
Master two foundational array and string patterns in C++ that reduce brute-force \( O(n^2) \) solutions to elegant \( O(n) \) algorithms.
What Is the Two-Pointer Pattern?
The two-pointer technique uses two index variables that traverse a data structure, often moving toward each other or in the same direction, to solve problems in \( O(n) \) time instead of \( O(n^2) \).
What Is the Sliding Window Pattern?
A sliding window maintains a contiguous sub-range of the array. Instead of re-computing from scratch, you slide the window forward: add the new element entering the right side and remove the element leaving the left side.
Why Do These Patterns Matter?
Core Rule: Two-Pointer on Sorted Data
Given a sorted array and a target value, place one pointer at the start and one at the end. If the current sum is too small, advance the left pointer. If too large, retreat the right pointer.
Core Rule: Sliding Window Template
The general sliding window maintains a range \[L, R\]. Expand \( R \) to include more elements, and shrink \( L \) when a constraint is violated.
Two-Pointer vs Sliding Window
| Two-Pointer | Sliding Window | |
|---|---|---|
| Pointer Movement | Opposite ends inward or same direction | Both move left-to-right |
| Data Requirement | Often sorted | Usually unsorted is fine |
| Typical Problems | Pair sum, palindromes, merging | Max/min subarray, substring constraints |
| Window State | No aggregate state needed | Running sum, frequency map, etc. |
| Complexity | \( O(n) \) time, \( O(1) \) space | \( O(n) \) time, \( O(1) \) to \( O(k) \) space |
How to Recognize & Apply
Example 1: Two Sum (Sorted Array)
Problem: given a sorted array, find two numbers that add up to target \( = 12 \).
Two Sum: Step-by-Step
{0, 5}Example 2: Max Sum of Size \( k \) Window
Problem: find the maximum sum of any contiguous subarray of length \( k = 3 \).
Example 3: Longest Substring Without Repeats
Problem: find the length of the longest substring with all unique characters in "abcabcbb".
Variable Window: The Code
Common Mistakes to Avoid
if may leave the window invalid.Pattern Summary
| Aspect | Opposite Two-Pointer | Fixed Window | Variable Window |
|---|---|---|---|
| Pointers | L at start, R at end | L and R move together | R always advances, L catches up |
| Pre-sort? | Yes | No | No |
| State | None | Running aggregate | Map/set + aggregate |
| Use Case | Pair sum, triplet | Max sum of size \( k \) | Longest valid substring |
| Time | \( O(n) \) | \( O(n) \) | \( O(n) \) |
Question 1 of 5
What is the time complexity of finding a pair with a given sum in a sorted array using the two-pointer technique?
Question 2 of 5
In a fixed-size sliding window of size \( k \), how do you update the window sum when sliding one position to the right?
Question 3 of 5
When using opposite-direction two-pointer for pair sum, why must the array be sorted?
Question 4 of 5
Consider this C++ function that attempts to find the longest substring without repeating characters:
What is the bug in this code?
if only removes one element from the left. If the duplicate is further inside the window, one shrink step is not enough. You need while (seen.count(s[R])) to keep shrinking until the duplicate is removed.Question 5 of 5
You need to find the minimum length subarray whose sum is at least \( S \) in an array of positive integers. Which pattern is most appropriate?