ReviseAlgo Logo
MediumVery High Frequency30 min

Two Pointers

Solve pair and subarray problems in O(n) by managing two indices simultaneously.

Use two indices moving toward each other (or same direction) to solve pair/subarray problems in O(n) instead of O(n²).

The Two Pointers Technique

Operations — Step by Step

Two Sum (sorted)

O(n)Requires sorted array. Converge from both ends.
Step-by-step1 / 4
L
2
0
marker
7
1
marker
11
2
R
15
3

L=0, R=3. Target = 9

Left at val=2, Right at val=15. Compute sum = 2+15 = 17.