Skip to content
Veltoryx Technologies
ProProblem 25● MediumLeetCode #18 ↗4 min readUpdated 30 October 2026

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.

InputOutput
[1, 0, -1, 0, -2, 2], target 03 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…