In the **Leetcode 3Sum Closest problem solution in Java programming** 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.

You may assume that each input would have exactly one solution.

## Leetcode 3Sum Closest problem solution in Java programming

```
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int minDiff = Integer.MAX_VALUE;
int result = 0;
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1, right = nums.length - 1;
while (left < right) {
int curSum = nums[i] + nums[left] + nums[right];
// If current sum has a smaller difference to the target, set minDiff to a new value and set result as the current sum
if (Math.abs(target-curSum) < minDiff) {
result = curSum;
minDiff = Math.abs(target-curSum);
}
if (curSum == target) {
return curSum;
} else if (curSum > target) {
right--;
} else {
left++;
}
}
}
return result;
}
}
```

**Also read,**

- Leetcode 3Sum Closest problem solution in C++
- Leetcode 3Sum Closest problem solution in C
- Leetcode 3Sum Closest problem solution in Python
- Leetcode 3Sum Closest problem solution in C#