Given an array of unsorted numbers and a target number, find a triplet in the array whose sum is as close to the target number as possible, return the sum of the triplet. If there are more than one such triplet, return the sum of the triplet with the smallest sum.
Below is the code -
import java.util.*;
class TripletSumCloseToTarget {
public static int searchTriplet(int[] arr, int targetSum) {
if (arr == null || arr.length < 3)
throw new IllegalArgumentException();
Arrays.sort(arr);
int smallestDifference = Integer.MAX_VALUE;
for (int i = 0; i < arr.length - 2; i++) {
int left = i + 1, right = arr.length - 1;
while (left < right) {
// 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];
if (targetDiff == 0) // we've found a triplet with an exact sum
return targetSum; // return sum of all the numbers
// the second part of the above 'if' is to handle the smallest sum when we have more than one solution
if (Math.abs(targetDiff) < Math.abs(smallestDifference)
|| (Math.abs(targetDiff) == Math.abs(smallestDifference) && targetDiff > smallestDifference))
smallestDifference = targetDiff; // save the closest and the biggest difference
if (targetDiff > 0)
left++; // we need a triplet with a bigger sum
else
right--; // we need a triplet with a smaller sum
}
}
return targetSum - smallestDifference;
}
public static void main(String[] args) {
System.out.println(TripletSumCloseToTarget.searchTriplet(new int[] { -2, 0, 1, 2 }, 2));
System.out.println(TripletSumCloseToTarget.searchTriplet(new int[] { -3, -1, 1, 2 }, 1));
System.out.println(TripletSumCloseToTarget.searchTriplet(new int[] { 1, 0, 1, 1 }, 100));
}
}
The piece of code that I couldn't understand is below :
if (Math.abs(targetDiff) < Math.abs(smallestDifference)
|| (Math.abs(targetDiff) == Math.abs(smallestDifference) && targetDiff > smallestDifference))
More specifically the expression (Math.abs(targetDiff) == Math.abs(smallestDifference) && targetDiff > smallestDifference)). Suppose my previous smallest difference is -2 and in next step my targetDiff comes out to be 2 . Then why I am updating my smallest difference to targetDiff .
For instance my sum of triplets are say - 3,4,5 and my targetSum value is 8. Then the targetDiff comes out to be (5,4,3) and the smallestDifference is 3. That makes sense but in case of equality I couldn't get the logic. Please help.