Back to problems

3Sum Problem

Algorithm · Meta · Medium

Given an array of integers, identify every distinct three-element combination (a, b, c) whose total is zero, while avoiding auxiliary storage. Values in the input may be positive, negative, or zero. When the array contains at most 3000 elements, target an O(n^2) running time. Input: [-2, 0, 2, 3, -2, -3] Output: [[-3, 0, 3], [-2, 0, 2]] The ordering of both the returned triplets and the numbers inside any triplet is irrelevant. Example The first triplet totals -3 + 0 + 3 =…

Checking your access…