# Question

Formatted question description: https://leetcode.ca/all/16.html

16	3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
Return the sum of the three integers. You may assume that each input would have exactly one solution.

For example, given array S = {-1 2 1 -4}, and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

@tag-array



# Algorithm

This question allows us to find the sum of the three numbers closest to the given value. It is a little more difficult on the basis of the previous 3Sum.

Then this question requires to return the value that is closest to the given value. Ensure that the absolute value of the difference between the current three numbers and the given value is the smallest, so you need to define a variable diff to record the absolute value of the difference, and then you still have to sort the array first, and then start traversing the array, the idea follows that The sum of the three numbers is very similar.

• First determine one number, and then use the two pointers left and right to slide to find the other two numbers.
• For every two numbers determined, find the sum of the three numbers, and then calculate and give the absolute value of the value difference is stored in newDiff,
• and then compared with diff and updated diff and the result closest, the code is as follows:

Java