Given an integer array `nums` of length `n` and an integer `target`, find three integers at distinct indices in `nums` whose sum is closest to `target`. Return that sum. You may assume each input has exactly one closest sum.
Given an integer array nums of length n and an integer target, find three integers at distinct indices in nums whose sum is closest to target. Return that sum. You may assume each input has exactly one closest sum.
Example 1:
Input: {"nums":[-1,2,1,-4],"target":1}
Output: 2
Constraints:
3≤nums.length≤500
−1000≤nums[i]≤1000
−104≤target≤104
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[-1,2,1,-4],"target":1}
Sort the array: [-4, -1, 1, 2]. Sorted order lets two pointers steer the sum up or down.