patternlens©
01Home02Patterns03Questions04Cheatsheet05Notes06Roadmaps07Dashboard

© 2026 PatternLens

patternlens©
Explore
HomePatternsQuestionsCheatsheetNotes
patternlens©
01Home02Patterns03Questions04Cheatsheet05Notes06Roadmaps07Dashboard

© 2026 PatternLens

patternlens©
Explore
HomePatternsQuestionsCheatsheetNotes
Cheatsheet›Two Pointers
CORE PATTERNHigh Frequency

Two Pointers

Use two indices that traverse a data structure — either toward each other, in the same direction, or across two separate arrays — to solve problems in linear time without extra space.

TimeO(N)
SpaceO(1)
DifficultyMedium

Key Insight

Two indices moving with purpose, not brute force.

Flip ⟲

Flip for Infographic

Interactive infographic on the flip side.

Click anywhere to Flip ⟲
FLIP SIDETwo Pointers
Click anywhere to flip back ⟲
Two Pointers Cheatsheet

Recognition Signals (When to use this pattern)

  • →Sorted array with pair/triplet target sum
  • →Palindrome or symmetry check
  • →Cycle detection in linked list
  • →Contiguous subarray or substring with constraint
  • →In-place rearrangement without extra space
  • →Two sorted arrays to merge or compare
1

Types of Problems (Subpatterns)

Each subpattern solves a specific type of problem. Click on a card to view its template.

Opposite Ends

L and R start at both ends and walk toward each other.

Common Problems

  • • Two Sum II
  • • 3Sum
  • • Container With Most Water
  • • Valid Palindrome
  • • Trapping Rain Water
5 problems

Fast & Slow (Floyd's)

Slow moves 1 step, fast moves 2 — they meet only inside a cycle.

Common Problems

  • • Linked List Cycle
  • • Linked List Cycle II
  • • Middle of the Linked List
  • • Happy Number
  • • Palindrome Linked List
5 problems

Partition (Slow / Fast Write)

Slow is the write head; fast scans and places valid elements.

Common Problems

  • • Remove Duplicates from Sorted Array
  • • Move Zeroes
  • • Sort Colors (Dutch National Flag)
  • • Remove Element
  • • Partition Labels
5 problems

Merge Two Arrays

One pointer per sorted array — pick the smaller element each step.

Common Problems

  • • Merge Sorted Array
  • • Intersection of Two Arrays II
  • • Merge Two Sorted Lists
  • • Median of Two Sorted Arrays
  • • Backspace String Compare
5 problems
2

Template / Approach

Template for: Opposite Ends

1

Place Pointers

Set L = 0 and R = n - 1 at opposite ends of the sorted array.

2

Evaluate Condition

Check the condition on arr[L] and arr[R] (e.g. their sum vs target).

3

Move Right Pointer

If the combined value is too large, move R left to decrease it.

4

Move Left Pointer

If the combined value is too small, move L right to increase it.

5

Stop When Crossed

Loop exits when L >= R — all pairs have been considered.

template_cpp
💡 Universal opposite-ends skeleton. Works for any sorted-array pair/triplet problem. Replace the comparison and return logic with your problem's goal.
1// Opposite-ends two-pointer skeleton
2void oppositeEnds(vector<int>& arr)
3{
4   int L = 0;
5   int R = (int)arr.size() - 1;
6
7   while (L < R)
8   {
9      int val = COMBINE(arr[L], arr[R]); // e.g. arr[L] + arr[R]
10
11      if (val == TARGET)
12      {
13         // ── found answer ─────────────────────────
14         RECORD(L, R);
15         L++;
16         R--;
17      }
18      else if (val < TARGET)
19      {
20         L++; // need a larger value
21      }
22      else
23      {
24         R--; // need a smaller value
25      }
26   }
27}
Key Idea:Sorted order guarantees: moving L right increases the sum, moving R left decreases it — so each step eliminates one possibility in O(1).

Practice Problems

Sharpen your skills with hand-picked problems.

View All Problems
Medium

Two Sum II - Input Array Is Sorted

LeetCode #167

Medium

3Sum

LeetCode #15

Medium

Container With Most Water

LeetCode #11

Easy

Valid Palindrome

LeetCode #125

Hard

Trapping Rain Water

LeetCode #42

Medium

4Sum

LeetCode #18

Easy

Linked List Cycle

LeetCode #141

Medium

Linked List Cycle II

LeetCode #142

Easy

Middle of the Linked List

LeetCode #876

Easy

Happy Number

LeetCode #202

Easy

Palindrome Linked List

LeetCode #234

Easy

Remove Duplicates from Sorted Array

LeetCode #26

Easy

Move Zeroes

LeetCode #283

Medium

Sort Colors

LeetCode #75

Easy

Remove Element

LeetCode #27

Medium

Remove Duplicates from Sorted Array II

LeetCode #80

Easy

Merge Sorted Array

LeetCode #88

Easy

Intersection of Two Arrays II

LeetCode #350

Easy

Backspace String Compare

LeetCode #844

Hard

Median of Two Sorted Arrays

LeetCode #4

patternlens.

See the patterns. Master the algorithms. Ace the interviews.

Visual DSA pattern library built to turn complex algorithms into clear, intuitive structures.

Learn

  • Patterns
  • Questions
  • Notes
  • Cheatsheet

Build

  • Roadmaps
  • Notes

© 2026 PatternLens. All rights reserved.

Designed & built with care

FAQsPrivacy PolicyCookies Policy