Medium
3Sum Closest
Medium
108 submissions
25 coins
+100 XP
Array
Sorting
Two Pointers
Problem Description
Pick three elements at distinct indices of `nums` whose sum is as close as possible to `target` and return that sum. The answer is guaranteed to be unique.
**Example 1:**
```
Input: nums = [-1, 2, 1, -4], target = 1
Output: 2
```
**Example 2:**
```
Input: nums = [0, 0, 0], target = 1
Output: 0
```
**Example 3:**
```
Input: nums = [1, 1, 1, 0], target = 100
Output: 3
```
Constraints
- `3 <= nums.length <= 500`
- `-1000 <= nums[i] <= 1000`
- `-10^4 <= target <= 10^4`
Need help?
Connect with expert programmers for real-time collaborative coding, video meetings, and whiteboard sessions via CodeConnect.
Video Call
Whiteboard
Live Coding
Screen Share