On this page
3Sum
Solving 3Sum using Two pointers.
Problem
Find triplets summing to zero.
Pattern
This problem demonstrates the Two pointers pattern.
Approach
Sort array, fix one element, two-pointer scan.
Solution
// Solution for 3Sum
// Pattern: Two pointers
// O(n²) time
Complexity
O(n²) time
Best Practices
- Identify the pattern before coding — pattern recognition saves time
- Handle edge cases: empty input, single element, duplicates
- Use descriptive variable names even in timed interviews
- Test with the provided examples plus one custom case