I’m a little confused on the comment explanation for finding the target diff rather than the current sum of the three pointer values as seen here:
// comparing the sum of three numbers to the ‘targetSum’ can cause overflow
// so, we will try to find a target difference
int targetDiff = targetSum - arr[i] - arr[left] - arr[right];
I assume the concern is if you add arr[i], arr[left], arr[right] you could end up with a value larger than Integer.MAX_VALUE? But by subtracting instead, aren’t you still at risk of overflowing the other way, and going below Integer.MIN_VALUE? Why not just assign the sum of the three to a long instead to be safe, or add some overflow detection logic if necessary?
I find it more intuitive to think through the solution when being able to compare the sum to the target, so was curious if there was something I was missing here and you really need to use the diff this way.