0% completed
// the second part of the following 'if' is to handle the smallest sum when we ...
Learner
Jan 7, 2022
// the second part of the following 'if' is to handle the smallest sum when we // have more than one solution
Isn't the second part unnecessary? Smallest is smallest, nothing changes even if they are the same. Can you please explain with an example if I have misunderstood?
3
0
Comments
Design Gurus4 years ago
This problem is asking to find a triplet with two conditions. 1) Closest to the target 2) If there are more than one such triplet, find the triplet with the smallest sum.
For example: Array [0, 0, 1, 1, 2, 6], Target = 5
We have two solutions here: (1, 1, 2) & (0, 0, ...
Learner 4 years ago
Awesome thanks for the explanation!
Thilak G4 years ago
Why cant the closest difference be in the -ve ? The below condition - can somebody explain it's purpose please - Math.abs(target_diff) === Math.abs(smallest_difference) && target_diff > smallest_difference)
Adam Seery3 years ago
i think adding this as a 4th example would be really helpful. thanks for the explanation
Design Gurus3 years ago
Adam Seery , sure.
Ethan Shook3 years ago
Thanks for the explanation, I didn't want to move on before understanding the intention of that line. A larger sum of the three pointers (index, left, and right) would result in a smaller or further negative number when subtracted from the target if I'm getting it corre...
On this page
Problem Statement
Try it yourself