dsa · medium
Three Sum Closest
Pick three values in nums whose sum is as close as possible to target. Return that sum. The answer is unique.
Arguments
nums— array of integerstarget— sum you want to approach
Example
nums = [-1,2,1,-4], target = 1 → 2 from -1 + 2 + 1.
Constraints
3 <= nums.length <= 500 -1000 <= nums[i], target <= 1000 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [-1,2,1,-4] 1 Expected: 2
Example 2
Input: [0,0,0] 1 Expected: 0