4Sum
Find every unique quadruplet that reaches a target with two anchors and a two-pointer squeeze, adding in long to avoid overflow.
01The problem
Find all unique quadruplets [a, b, c, d] at different positions with a + b + c + d = target. Values can be as large as 10⁹, so the sum of four can overflow an int.
| Input | Output |
|---|---|
[1, 0, -1, 0, -2, 2], target 0 | 3 quadruplets |
[2, 2, 2, 2, 2], target 8 | [[2, 2, 2, 2]] |
Constraints: 1 to 200 values, each between −10⁹ and 10⁹.
Loading the rest of the lesson…