Description

Two pointer converging is when you have two pointers starting at each end and they are working their way to the middle.
?
Compare the two ends and move the pointer that can't improve the answer — one O(n) pass instead of checking every pair.

Reach for when

Sorted (or symmetric) input where you need a pair/target or to optimize between the two ends using something like Prefix Postfix.

Runtime

O(n)

Visualization

Two Pointer-1783450848388.webp439

Pseudocode

l, r = 0, len(arr) - 1

while l < r:
	cur = evaluate(arr[l], arr[r])
	if cur meets the goal:
		return (l, r)
	elif cur is too small:
		l += 1
	else:
		r -= 1
return not found

Code

def two_sum_sorted(nums, target):
	l, r = 0, len(nums) - 1
	while l < r:
		total = nums[l] + nums[r]
		if total < target:    # too small → need a bigger left value
			l += 1
		elif total > target:  # too big → need a smaller right value
			r -= 1
		else:                 # found the pair
			return [l, r]
	return []