Skip to content

Foundations -> Two Pointers

Exploit monotone movement to replace nested loops with linear scans, sliding windows, or opposite-end sweeps.

  • Topic slug: foundations/two-pointers
  • Tutorial page: Open tutorial
  • Ladder page: Open ladder
  • Repo problems currently tagged here: 4
  • Repo companion pages: 3
  • Curated external problems: 6

Microtopics

  • opposite-ends
  • sliding-window
  • sorted-two-sum
  • monotone-window
  • amortized-linear
  • longest-valid-segment

Learning Sources

Source Type
USACO Guide two pointers Reference
Competitive Programmer's Handbook Reference

Practice Sources

Source Type
Codeforces two-pointers tag Practice
CSES Problem Set Practice

Repo Companion Material

Material Type
Foundations cheatsheet quick reference
DP cheatsheet adjacent reference
Templates overview template guide

Curated External Problems

Core

Problem Source Difficulty Context Style Prerequisites Tags Why it fits
Playlist CSES Medium Sliding Window Implementation-Heavy; Proof-Heavy Two Pointers; Hash Maps; Window Invariants Distinctness; Subarrays; Distinct A clean sliding-window benchmark where the main skill is maintaining the window invariant.
Subarray Sums I CSES Medium Sliding Window Implementation-Heavy Two Pointers; Positive Numbers; Window Sums Subarrays; Positive-Sums; Positive-Array A standard positive-array window problem that helps lock in the expand-shrink pattern.
Sum of Two Values CSES Medium - Implementation-Heavy Sorting; Hash Maps; Pair Sums Pair-Sum; Hash-Map A common first pair-sum problem that can be solved with either hashing or sorted two pointers.

Variants

Problem Source Difficulty Context Style Prerequisites Tags Why it fits
Sliding Window Distinct Values CSES Medium - Implementation-Heavy Two Pointers; Frequency Maps; Window Maintenance Sliding-Window; Frequency-Counting; Distinct-Count A direct sliding-window variant that reinforces the same pattern with a slightly different output.

Classics

Problem Source Difficulty Context Style Prerequisites Tags Why it fits
Ferris Wheel CSES Easy - Greedy-Heavy Sorting; Two Pointers; Greedy Matching Pairing A textbook two-pointer pairing problem that is easy to teach and easy to reuse.
Distinct Values Subarrays CSES Medium Sliding Window Proof-Heavy; Implementation-Heavy Two Pointers; Frequency Counts; Subarray Counting Distinctness; Distinct-Count A classic counting-by-window problem that builds strong two-pointer intuition.

Repo Problems

Code Title Fit Difficulty Pattern Note Solution
APARTMENTS Apartments primary easy-medium two sorted pointers; tolerance matching; greedy monotone scan Note Code
FERRISWHEEL Ferris Wheel secondary easy-medium opposite end pointers; greedy pairing; sorted capacity matching Note Code
SUMOFTWOVALUES Sum of Two Values primary medium sort and scan; opposite end pointers; index restoration Note Code
TFIELD Ruộng bậc thang secondary hard nested polygons; weighted sliding window; shoelace preprocessing Note Code

Regeneration

python3 scripts/generate_problem_catalog.py