92. 3Sum Closest

MediumArrayArray

Given an integer array nums of length n and an integer target, find three integers whose sum is closest to target. Return the sum of the three integers.

Input: Integer array nums and integer target.

Output: Integer — sum of three numbers closest to target.

Examples

Example 1
Input: [-1,2,1,-4],1
Output: 2
Explanation: Sum of [-1,2,1]=2. Closest to 1.
Example 2
Input: [0,0,0],1
Output: 0
Explanation: Only triplet is [0,0,0]=0.
Example 3
Input: [1,2,3],0
Output: 6
Explanation: Only triplet is [1,2,3]=6.

Constraints

Asked by

BloombergAmazonGoogleMetaFlipkartMicrosoft
Solve this problem in the editor →