3Sum - Find All Triplets with Zero Sum
viaGlassdoor
Problem: Given an array of integers, find all unique triplets [a, b, c] such that a + b + c = 0.
Constraints: The array may contain duplicates; the result must not contain duplicate triplets.
Example: Input: [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]]
Approach: Sort the array, then for each index i, use two pointers (left = i+1, right = n-1) to find pairs summing to -arr[i], skipping duplicate values for i, left, and right to avoid repeated triplets. This runs in O(n^2) time and O(1) extra space (excluding output).
asked …