Given an array of integers, A1, A2, ..., An, including negatives and positives, and another integer S. Now we need to find three different integers in the array, whose sum is closest to the given integer S. If there exists more than one solution, any of them is ok.
You can assume all the integers are within int32_t range, and no arithmetic overflow will occur with calculating the sum. S is nothing special but a randomly picked number.
Is there any efficient algorithm other than brute force search to find the three integers?
Another solution that checks and fails early:
I added some unit tests here: GivenArrayReturnTrueIfThreeElementsSumZeroTest.
If the set is using too much space I can easily use a java.util.BitSet that will use O(n/w) space.