Skip to main content
Two Pointer

Converging Pointers

One pointer starts at the beginning, the other at the end. They move inward based on a condition. Perfect for sorted array pair problems like 'find two numbers that sum to target'.

O(n)
·
O(1)

How It Works

Converging pointers start at opposite ends of a sorted array and walk toward each other. At each step, you compare the pair they point to against the target: if the sum is too small, only advancing the left pointer can increase it; if too large, only retreating the right pointer can decrease it. Each move safely discards one element from consideration.

That one-directional elimination is why the technique beats brute force. Checking all pairs costs O(n²), but here the pointers together make at most n moves, giving O(n) time and O(1) space. The prerequisite is sortedness (or another monotonic structure) so each comparison tells you unambiguously which pointer to move.

Step-by-Step Visualization

Target = 10. Place pointers at both ends
L
1
0
3
1
4
2
6
3
8
4
R
11
5
Sum1 + 11 = 12
Target10
1/6

Code

Java
static int[] twoSumSorted(int[] nums, int target) {
  int left = 0;
  int right = nums.length - 1;

  while (left < right) {
    int sum = nums[left] + nums[right];

    if (sum == target) {
      return new int[]{left, right}; // Found!
    } else if (sum < target) {
      left++;  // Need bigger sum → move left right
    } else {
      right--; // Need smaller sum → move right left
    }
  }

  return new int[]{}; // No pair found
}

// Example: twoSumSorted(new int[]{1, 3, 4, 6, 8, 11}, 10)
// Answer: [2, 3] → nums[2]+nums[3] = 4+6 = 10

Tips & Gotchas

1Array MUST be sorted for this to work
2If sum is too small, move left pointer right (increase sum)
3If sum is too large, move right pointer left (decrease sum)
4Can be extended to 3Sum by fixing one element and doing 2Sum on the rest

Practice Problems

  • 1Two Sum II - Input Array Is Sorted
  • 23Sum
  • 3Container With Most Water
  • 4Valid Palindrome

About the Two Pointer Pattern

Use two index variables that move through the array strategically. They might start at opposite ends and converge, or both start at the beginning with one moving faster. This avoids nested loops.

Key insight

When you see 'subarray', 'contiguous', or 'in-place', think arrays. The key is reducing brute-force O(n²) to O(n) using sliding window, two pointers, or prefix sums.

Common Array Interview Problems

  • Two Sum
  • Best Time to Buy & Sell Stock
  • Maximum Subarray
  • Merge Intervals
  • Product of Array Except Self
  • Container With Most Water

Frequently Asked Questions

When do converging pointers beat a hash map for pair-sum problems?

If the array is already sorted, converging pointers use O(1) extra space versus the hash map's O(n), and they naturally enumerate pairs in order, which helps when you must skip duplicates as in 3Sum. On unsorted input, the hash map wins because sorting first would cost O(n log n).

How do I know it's safe to move a pointer and not skip the answer?

Moving a pointer discards all pairs involving the abandoned element, so you need an argument that none of them can be optimal. In Container With Most Water, the shorter line caps every pairing it could form with the elements between the pointers, so discarding it loses nothing.