Patterns

Concept shape

Practice pattern
0
1
2
3
4
5
Definition

Use two indexes to scan from different positions without nested loops.

Step 1 / 5
Concept shape
Current lesson

Two Pointer Pattern Lab

Patterns Practice pattern

Two Pointer Pattern

Welcome to the Two Pointer Pattern concept lab. Use two indexes to scan from different positions without nested loops. The must know points are: Often requires sorted input. Pointers move based on a condition. Can reduce O(n^2) to O(n). The common operations are: left++, right--, compare pair, shrink region. The complexity shape is Usually O(n). Watch out for these pitfalls: Moving the wrong pointer. Forgetting that unsorted input may break the pattern. Related algorithms include Two Sum Sorted, Palindrome Check.

Concept details

Use two indexes to scan from different positions without nested loops.

Must know

  • Often requires sorted input.
  • Pointers move based on a condition.
  • Can reduce O(n^2) to O(n).

Complexity

Usually O(n)

Open deeper visual structure lesson
1.0x