Solution 1: Sort the set first. For any element y in S, use binary search to determine whether there is one element z in S-y such that y + z = x. Since the binary search can be done Θ(lg n), the overall running time is Θ(n lgn).
Solution 2: Sort the set first. Let be the sorted result. If
> x, then the solution must be in
. If
< x, then the solution must be in
. If
= x, we are done. So we can solve the problem recursively. The overall running time is Θ(n lgn).