3Sum Closest
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to the target.
Return the sum of the three integers.
You may assume that each input has exactly one solution.
Example 1
Input
4 -1 2 1 -4 1
Output
2
Explanation
The triplet (-1, 2, 1) has a sum of 2, which is the closest possible value to the target 1.
Example 2
Input
3 0 0 0 1
Output
0
Explanation
The only possible triplet is (0, 0, 0), whose sum is 0.
Constraints
Hints:
Hint 1
Sort the array before processing the triplets.
Hint 2
Fix one element and use two pointers to search for the closest sum. Keep track of the minimum absolute difference from the target throughout the traversal.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 5, 2026
Last updated
Aug 6, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output