You are given an array of integers, nums
, and another integer, target
. Your task is to identify three distinct elements from the array such that their combined sum is as close as possible to the value of target
.
Your solution should return this closest possible sum of the three elements.
You can assume there is always exactly one unique combination that results in the closest sum.
- Input:
nums = [-1, 2, 1, -4]
,target = 1
- Output:
2
- Explanation: The sum of the three numbers closest to 1 is 2, achieved by the combination
(-1 + 2 + 1 = 2)
.
- Input:
nums = [0, 0, 0]
,target = 1
- Output:
0
- Explanation: The closest sum is
0
, since0 + 0 + 0 = 0
.
To get started with solving this problem, follow these steps:
git clone https://github.com/devslopes/algo-1-closest-sum.git
cd closest
npm install
npm test