3Sum Closest Problem

3Sum Closest Problem — ExecCode Medium DSA Practice

Solve the 3Sum Closest problem on ExecCode. Free online medium DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Find the triplet whose sum is closest to target. Sort the array, fix one number, then move left or right based on whether the current sum is below or above target.

Examples

Input nums = [-1,2,1,-4], target = 1; Output 2. Input nums = [-5,-2,-1,0,1,3,7,9,11,14], target = 31; Output 30

Constraints

1 ≤ n ≤ 10^5

Practice 3Sum Closest free on ExecCode. Browse DSA problems, topic map, and placement guides.