Exercise 2.3-7 from "Introduction to Algorithms" by Cormen et al. Third Edition, states:
Describe a O(n lg n)-time algorithm that, given a set S of n integers and another integer x, determines whether of not there exist two elements in S whose sum is exactly x.
At first, I had no idea how to solve it since I thought you couldn't access elements of a set by index, but assuming you could, here was my solution:
First off, we sort the set S, and then for every element y in S, we search if x - y exists in S. If x - y exists then we are done, otherwise continue the process until we have looked at all elements in S.
Since we sort at the beginning it's O(n log n) and then we perform a binary search for every element, so total cost would be O(n log n) + O(n log n), therefore O(n log n).
But the solution they give in the Lecture Notes is:
The following algorithm solves the problem:
- Sort the elements in S.
- Form the set S'= {z : z = x − y for some y ∈ S}.
- Sort the elements in S'.
- If any value in S appears more than once, remove all but one instance. Do the same for S'.
- Merge the two sorted sets S and S'.
- There exist two elements in S whose sum is exactly x if and only if the same value appears in consecutive positions in the merged output.
And they go on to say:
Steps 1 and 3 require O(n lg n) steps. Steps 2, 4, 5, and 6 require O(n) steps. Thus the overall running time is O(n lg n).
So obviously they assume a set is just a regular array (you can access elements by index and the elements don't have to be unique). (And just to clarify my assumption, from the beginning of the book up to this point, they haven't put forth their definition of set, so it was easy to assume they were referring to an actual set).
It seems to me that even though their solution is O(n log n), my solution not only does it reduce the "hidden costs" greatly, it's also much more straightforward than theirs.
(To account for the possibility of repeated elements, nothing needs to be modified since the current y is not inside the searching subarray)
I've checked the published erratas (I know it wouldn't be a mistake per se anyway, but I thought it might've said something there), and there is nothing. So my question is: why is my solution wrong? Is my algorithm incorrect or is it my analysis?