Pattern Recognition Drill
Medium Two Pointers
The Problem
Given array nums, find all unique triplets that sum to zero.
What approach would you use?
Think about it before scrolling down.
Sort. Fix i, two pointers on [i+1..n-1]. Skip duplicate values of i, l, r. O(n²).
For each pair, check if -(a+b) exists. Harder to deduplicate. O(n²).
Common Trap
Deduplication: skip i if nums[i] == nums[i-1]. Skip l if nums[l] == nums[l-1] after finding a triple.