# Algorithmic Questions 丨 3Sum Closest

Source: Internet
Author: User
Tags abs

Describe

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

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).``
Algorithm analysis

Difficulty : Medium
Parse : Given an array of integers and a specified target integer value, 3 elements are found from the array, the sum of the 3 elements is closest to the target, and the result is the sum of the 3 elements.
idea : The general idea is similar to the 3Sum algorithm, the array is sorted first, and then began to traverse, 3 elements are currently traversed elements, clamping force start element is the default element behind the current element, clamp force end element of the default array of the last element, Clamping force is achieved by starting the element increment and decreasing the element by clamping force:
? 1. If the sum of the 3 elements is larger than the target value, it is necessary to lower the value of the clamping end element, so that it is possible to approach the target value, so the clamping force ends the element descending;
? 2. If the sum of the 3 elements is not greater than the target value, it is necessary to clamp the starting element value to become larger, it is possible to approach the target value, so the clamping force begins to increment the element;
At the end of this traversal, we then judge the nearest and the next 3 elements of the current record, take a closer as the return result, and then continue the traversal until the result is traversed, to obtain the return result.

Code samples (C #)
``public int ThreeSumClosest(int[] nums, int target){    int res = nums[0] + nums[1] + nums[nums.Length - 1];    //排序后遍历    Array.Sort(nums);    for (int i = 0; i < nums.Length - 2; i++)    {        //从当前后面的元素和最后一个元素，两边夹逼        int lo = i + 1, hi = nums.Length - 1;        while (lo < hi)        {            int sum = nums[i] + nums[lo] + nums[hi];            if (sum > target)            {                hi--;            }            else            {                lo++;            }            //如果此次遍历的3个元素的和更接近，则赋值返回的结果            if (Math.Abs(sum - target) < Math.Abs(res - target))            {                res = sum;            }        }    }    return res;}                                 ``
Complexity of
• time complexity :O (n2).
• space complexity :O (1).
Appendix
• Code implementation (C # Edition)
• Correlation algorithm
• The Sum of
• 3Sum
• 4Sum

Algorithmic Questions 丨 3Sum Closest

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.