Q&A-1How does the two-pointer technique achieve O(N) time complexity on sorted inputs compared to O(N^2) brute-force, and what core invariant must hold for it to be applicable?
Answer
The 60-second answer
Two pointers turns an $O(N^2)$ scan of all pairs into a single $O(N)$ pass by exploiting sortedness: each comparison lets you permanently discard one index, so no pair is ever revisited.
The invariant is monotonicity. With left at the low end and right at the high end of a sorted array, moving left right can only increase nums[left] + nums[right], and moving right left can only decrease it. That is what makes discarding safe.
Concretely, for a target sum: if nums[left] + nums[right] < target, then nums[left] is too small to pair with right or anything to its left. Every such pair is also below target. So left is dead; advance it. If the sum is too large, nums[right] is too big for left or anything to its right; retreat right. Each step removes one index from consideration, so the pointers cross after at most $N-1$ steps. Assumptions: the array is sorted ascending, we want one witness pair of distinct indices, and the evaluator (here, addition) is monotone in each pointer.
Trace: nums = [1, 3, 4, 6, 8, 11], target = 10:
| left | right | sum | action |
|---|---|---|---|
| 0 (1) | 5 (11) | 12 | > 10, retreat right |
| 0 (1) | 4 (8) | 9 | < 10, advance left |
| 1 (3) | 4 (8) | 11 | > 10, retreat right |
| 1 (3) | 3 (6) | 9 | < 10, advance left |
| 2 (4) | 3 (6) | 10 | match, return (2, 3) |
Five comparisons for six elements: linear, not the 15 pairs brute force would check.
def two_sum_sorted(nums: list[int], target: int) -> tuple[int, int] | None:
left, right = 0, len(nums) - 1
while left < right:
current = nums[left] + nums[right]
if current == target:
return left, right
if current < target:
left += 1 # nums[left] too small for any k <= right
else:
right -= 1 # nums[right] too big for any k >= left
return NoneFailure modes
- Non-monotonic evaluator. The discard argument needs the metric to move in one direction per pointer. For a product target with negatives, sorting does not give that: incrementing
leftpast a negative value raisesnums[left], which lowers the product against a positiverightbut raises it against a negativeright. The elimination step is unsound and the pass can miss the answer. - Enumerating all pairs. The linear bound is for finding one witness. If you must output every pair, the output itself can be $O(N^2)$, and duplicate handling has to advance pointers forward, never reset them backward, or the worst case degrades.
- Unsorted input. Sorting first costs $O(N \log N)$, which dominates the pass. If constant space is not required, a hash map gives $O(N)$ time and $O(N)$ space without sorting.
Detect a broken invariant by testing inputs where the metric is non-monotone (negatives, products, absolute values) and asserting the pass still finds a known witness.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.