Description
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers.
Examples
Input:
nums = [-1,2,1,-4], target = 1Output:
2Explanation:
The sum that is closest to 1 is -1 + 2 + 1 = 2.
Input:
nums = [0,0,0], target = 1Output:
0Explanation:
Edge case returning zero.
Input:
nums = [1,1,1,0], target = -100Output:
2Explanation:
When the target is much smaller than any possible sum, the goal is the smallest possible sum. The possible sums are: 1+1+1=3, 1+1+0=2, 1+1+0=2 (duplicate), 1+0+1=2 (duplicate). The smallest sum is 2, which is closest to -100.
Constraints
- •
3 ≤ nums.length ≤ 500 - •
-1000 ≤ nums[i] ≤ 1000 - •
-10⁴ ≤ target ≤ 10⁴