In: Computer Science
O(nlogn) algorithm to solve this problem:
1) Sort the set S if, they are not sorted
2) loop over the set and for each element a in the set:
1) Find b = z/a
2) Then, search for b in the set using Binary Search.
3) If found return true
4) return false
Time complexity analysis of algorithm
Lets say set has n elements,
sorting the array ------ O(nlogn) [use quick sort or merge sort or heap sort]
looping over the set ------ O(n). And for each element a in the set, we are finding a number b such that a*b = z. For this we will use Binay Search
Binary search ------- O(logn)
So total time complexity of looping over the set and searching for a number b such that a*b = z is O(n)*O(logn) = O(nlogn)
Total time complexity of algorithm = O(nlogn)sorting + O(nlogn)looping + searching = O(nlogn)