Arrays

Problem shape

Beginner
4
8
15
16
23
42
Core idea

Move two indexes through an array to shrink, expand, or compare a search region.

Step 1 / 5
Problem shape
Current lesson

Two Pointers Lab

Arrays Beginner

Two Pointers

Welcome to the Two Pointers algorithm lab. Move two indexes through an array to shrink, expand, or compare a search region. Use it when: Use on sorted arrays, pair-sum checks, partitions, and palindrome checks. The time complexity is O(n), and the space complexity is O(1). The pseudocode is: left = 0, right = n - 1 while left < right inspect a[left], a[right] move left or right based on condition As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n)

Space

O(1)

Pseudocode

left = 0, right = n - 1
while left < right
  inspect a[left], a[right]
  move left or right based on condition
Open deeper structure lesson
1.0x