Skip to main content
Back to problems
Leetcode
Medium
Arrays
Sorting
Two Pointers
Math
3Sum Closest

Find the triplet sum in an integer array that is closest to a target value.

Acceptance 90%
Problem Statement

Problem

Given an integer array nums and an integer target, choose three distinct elements from nums whose sum is as close as possible to target.

Return the sum of the chosen triplet.

If multiple triplets are equally close, any one of their sums is acceptable.

Goal

You are not asked to return the indices or the triplet itself, only the closest sum.

Input Format

  • nums: an array of integers
  • target: an integer

You may assume nums contains at least 3 numbers.

Output Format

  • Return a single integer: the sum of three numbers whose value is closest to target.

Constraints

  • Choose exactly 3 distinct elements from nums.
  • nums contains at least 3 integers.
  • The answer is guaranteed to fit in a 32-bit signed integer for typical interview settings.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [-1, 2, 1, -4], target = 1

Output

2

Explanation

The triplet [-1, 2, 1] has sum 2, which is closest to 1. Another valid example is not closer than 2.

Example 2

Input

nums = [0, 0, 0], target = 1

Output

0

Explanation

The only possible triplet sum is 0, so it is the closest.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.