IDE Workbench
Home / Patterns / Two Pointers

Two Pointers Pattern Hub

5 problems

3Sum

medium
TC: O(N^2) · SC: O(1)

Sort the array first (`O(N log N)`), fix anchor `nums[i]` (skipping duplicates and breaking early if `nums[i] > 0`), and run Two Pointers on `(i+1 .. N-1)` skipping duplicates on `l` after matches.

Container With Most Water

medium
TC: O(N) · SC: O(1)

Start with widest container `(l=0, r=n-1)`. Always move the SHORTER wall inward: advancing the taller wall can only shrink width while keeping height bounded by the untouched shorter wall.

Trapping Rain Water

hard
TC: O(N) · SC: O(1)

Water above bar `i` equals `min(maxLeft, maxRight) - height[i]`. Using two pointers maintaining `leftMax` and `rightMax`, whenever `leftMax <= rightMax`, the right side is guaranteed >= `leftMax`, so water at `l` is finalized cleanly in O(1) space!

Two Sum II - Input Array Is Sorted

medium
TC: O(N) · SC: O(1)

Sorted order guarantees monotonic sum adjustments: if `nums[l] + nums[r] > target`, only decreasing `r` can lower the sum; if `< target`, increment `l`. Return 1-based indices.

Valid Palindrome

easy
TC: O(N) · SC: O(1)

Converging left/right pointers skipping non-alphanumeric characters, comparing lowercased chars in O(1) extra space without allocating filtered strings.

Local-first (IndexedDB)Local
PrivacyTerms