2Brute2Best
🔥0

3Sum Closest

Medium·LeetCode #16 Pro OnlySlight Variation
LeetCode
01

Problem Statement

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

Pick three elements from three different positions. Among all such triplets, find the one whose sum is closest to target, and return that sum.

You may assume there is exactly one closest sum.

Input

An integer array nums and an integer target.

Output

One integer: the triplet sum with the smallest |sum − target|.

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

−1 + 2 + 1 = 2 is only 1 away from the target. No triplet sums to exactly 1.

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

The only triplet adds up to 0.

Constraints
  • • 3 ≤ nums.length ≤ 500
  • • −1000 ≤ nums[i] ≤ 1000
  • • −10^4 ≤ target ≤ 10^4
02

Pattern Recognition Clues

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

“three integers”
→ Fix one, pair the other two → k-sum family.
“closest to target”
→ No exact match to find, so keep a running best. The sign of `sum − target` still says which pointer to move.
“return the sum”
→ Positions don't matter, so sorting is fine.
03

Approach Complexity Ladder

Brute Force — every tripletO(N³)O(1)

Three loops; keep the sum with the smallest distance

Better — sort, fix two, binary search the thirdO(N² · log N)O(1)

Sort; for each pair, binary search the value closest to what's left

Optimal — sort, fix one, two pointersO(N²)O(1)

Sort; fix nums[i]; move left/right towards the target, track the closest

Brute Force Intuition

Check every triplet i < j < k. Keep the sum whose distance |sum − target| is smallest.

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.