A gentle introduction

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris non tortor a turpis fermentum porta. Curabitur eget tortor at velit fermentum porta.

Heads up. This is a placeholder chapter. Replace it with real content when authoring your course.

Why two pointers?

Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.

A worked example

Consider the classic pair sum problem on a sorted array:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def pair_sum_sorted(nums, target):
    """Return indices of two numbers in `nums` summing to `target`, or None."""
    left, right = 0, len(nums) - 1
    while left < right:
        s = nums[left] + nums[right]
        if s == target:
            return (left, right)
        if s < target:
            left += 1
        else:
            right -= 1
    return None

The animation below shows how the pointers converge:

Two-pointer animation placeholder

Mini chart

A quick comparison of brute-force vs. two-pointer time complexity:

Approach Time Space
Brute force O(n²) O(1)
Two-pointer O(n) O(1)

Going further

Wrap up

You’ve seen the core idea. In the next chapter we’ll apply it to strings. Don’t forget to mark this chapter complete when you’re done — your progress is saved locally in your browser.

Saved locally to your browser.