Back to Roadmap
Week 1Day 4 of 35

Two Pointers on Sorted Array

Use sorted order to decide whether to move left or right.

Day Progress0%

0 of 3 problems solved

Pattern Focus

Two Pointers on Sorted Array

Use sorted order to decide whether to move left or right.

Pattern Checklist
  • Is the array sorted?
  • Can the current sum tell me which pointer to move?
  • Am I moving only one pointer per step?
👉👈New to DSA? Start here 🧠

This is Two Sum again, but now the array is sorted. That sorted order is the whole gift. Put one pointer on the smallest number and one on the biggest. If the sum is too small, you need a bigger number, so move the left pointer. If the sum is too big, you need a smaller number, so move the right pointer. The sorted array tells you exactly which direction makes sense.

Sorting removes the guesswork. The current sum tells you exactly which pointer to move.

How to think about it
  1. 1Set left to the first index and right to the last index.
  2. 2Compute the sum of numbers at left and right.
  3. 3If the sum is too small, move left to the right.
  4. 4If the sum is too large, move right to the left.
  5. 5If the sum matches the target, return the answer.
🚧Common Mistake

Moving both pointers at once throws away information. Only one side should move, based on whether the sum is too small or too large.

🔍Problem Hints

3Sum Closest

Sort first, fix one pointer, scan with left/right. Track the smallest absolute difference from target and update on each iteration.

Median of Two Sorted Arrays

Binary search on the smaller array to find the partition where combined left halves have exactly half the total count and cross-boundary values stay in order.

Problems

Two Sum II

easy

3Sum Closest

medium

Median of Two Sorted Arrays

hard