2Brute2Best
🔥0

4Sum

Medium·LeetCode #18 Pro OnlyPattern Transfer
LeetCode
01

Problem Statement

You are given an integer array nums and a number target.

Find every group of four values, taken from four different positions, whose sum is exactly target.

Each group may appear only once: two groups with the same four values count as the same, even if they came from different positions. The order of the groups, and of the values inside them, does not matter.

Input

An integer array nums and an integer target.

Output

A list of all unique quadruplets (lists of 4 values) that add up to target.

Example 1
Input: nums = [1, 0, -1, 0, -2, 2], target = 0
Output: [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]

These are the only three different groups of values that add up to 0. Some can be built from different positions (there are two 0s), but each is listed once.

Example 2
Input: nums = [2, 2, 2, 2, 2], target = 8
Output: [[2, 2, 2, 2]]

Many position-groups give 2 + 2 + 2 + 2, but it is one answer.

Constraints
  • • 1 ≤ nums.length ≤ 200
  • • −10^9 ≤ nums[i] ≤ 10^9
  • • −10^9 ≤ target ≤ 10^9
  • • A sum of four values can reach ±4·10^9, which does not fit in an `int`.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“four values that add up to target”
→ Fix two, then Two Sum II on the rest → k-sum with k = 4.
“unique quadruplets”
→ Sort and skip equal neighbours at every fixed level and after every hit.
“values up to 10^9”
→ The sum of four overflows `int` → use `long long`.
03

Approach Complexity Ladder

Brute Force — every quadruplet + a setO(N⁴ · log M)O(M)

Four loops; store each matching group sorted in a set

Better — fix three, hash set for the fourthO(N³ · log M)O(N + M)

Fix a, b; for each c, look up the needed fourth value in a set

Optimal — sort, fix two, two pointers, skip duplicatesO(N³)O(1)

Sort; fix a and b (skipping repeats); two pointers for the last pair

Brute Force Intuition

Try every a < b < c < d. When the four values add up to target, sort them and put them in a set, which drops repeats.

Recognition rule for this sub-pattern:

Sort, fix one element (or more), and solve the rest as a two-pointer pair problem. Skip duplicates carefully.
Remember this · Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.