Describe a θ(nlgn)-time algorithm that, given a set S of n real numbers and another real number x, determines whether or not there exist two elements in S whose sum is exactly x.
Solution:
First, sort the set using any θ(nlgn)-time sorting algorithm.
Then for each real number a in set S, search if (x-a) exists in the set S using binary seach algorithm.
It's easy to show that this solution is a θ(nlgn)-time algorithm.
Solution:
First, sort the set using any θ(nlgn)-time sorting algorithm.
Then for each real number a in set S, search if (x-a) exists in the set S using binary seach algorithm.
It's easy to show that this solution is a θ(nlgn)-time algorithm.