Easy → Hard Pattern 1 of 14

Two Pointers

From absolute beginner to advanced — interactive lesson with live examples

Lesson
Try it yourself
Code explained

Array: [2, 5, 8, 11, 14]  |  Walking through 3 examples

Press Next Step to begin.

Try with your own numbers

Every line in plain English

left, right = 0, len(arr) - 1
Place left finger at position 0 (start of array). Place right finger at the last position. These are your two pointers.
while left < right:
Keep looping as long as left finger has not crossed right finger. When they meet — stop. We have checked all useful pairs.
s = arr[left] + arr[right]
Add the value under the left finger plus the value under the right finger. Store it in s.
if s == target:
    return [left, right]
If the sum matches what we want — we found the answer! Return both positions.
elif s < target:
    left += 1
Sum is TOO SMALL. We need a bigger number. Move left finger one step RIGHT (towards bigger values in the sorted array).
else:
    right -= 1
Sum is TOO BIG. We need a smaller number. Move right finger one step LEFT (towards smaller values).
Why is this O(n) not O(n²)?
Each pointer can only move inward — left can move right at most n times, right can move left at most n times. Total moves = at most 2n = O(n). A two-loop approach checks every pair = n² checks. For n=10,000: two pointers = 20,000 operations vs nested loops = 100,000,000 operations.

The Core Idea

Two pointers works because the array is sorted. This gives us a superpower:

So when our sum is too small, we know exactly what to do — move left pointer right to get a bigger number. When too big, move right pointer left. We never need to backtrack or check everything.

The key insight: On a sorted array, if arr[left] + arr[right] is too small, then arr[left] + arr[right-1] is also too small (since arr[right-1] < arr[right]). So we can safely skip arr[left] with any right pointer smaller than the current right. This is why we never miss any valid pair.

Step-by-Step Walkthrough

arr = [2, 5, 8, 11, 14]   target = 13

Initial:   L=0(2)   R=4(14)
           [2,  5,  8, 11, 14]
            L               R

Step 1:  2 + 14 = 16 > 13  TOO BIG  → R moves left
           [2,  5,  8, 11, 14]
            L           R

Step 2:  2 + 11 = 13 = 13  MATCH ✅ → return [0, 3]
           [2,  5,  8, 11, 14]
            L        R

LeetCode Problems — Solve in This Order

#ProblemDifficultyKey learning
167Two Sum IIEasyClassic template — start here
125Valid PalindromeEasyBoth pointers move inward checking chars
153SumMediumFix one element, two pointers on the rest
11Container With Most WaterMediumMaximise area by moving shorter side
42Trapping Rain WaterHardTrack max height from both sides

Copy-Paste Template

def two_sum_sorted(arr, target):
    left, right = 0, len(arr) - 1

    while left < right:
        current_sum = arr[left] + arr[right]

        if current_sum == target:
            return [left, right]       # found answer
        elif current_sum < target:
            left += 1                  # need bigger — go right
        else:
            right -= 1                 # need smaller — go left

    return [-1, -1]                    # no pair found