Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

3Sum Closest

Medium
two-pointersarraysorting

Given an integer array nums and an integer target, find three integers in nums whose sum is closest to target and return that sum. Each input has exactly one answer.

Input

  • Line 1: space-separated integers nums
  • Line 2: integer target

Output: the closest achievable sum.

Example 1
Input:
-1 2 1 -4
1
Output:
2

-1 + 2 + 1 = 2 is closest to 1.

Example 2
Input:
0 0 0
1
Output:
0

Constraints

  • 3 ≤ nums.length ≤ 500
  • -1000 ≤ nums[i] ≤ 1000
  • Exactly one answer is closest.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.