Describe an algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. Your algorithm must be nlogn. Evaluate how long each step in the algorithm takes to demonstrate that it meets this requirement.
sort in worst case take O(nlogn)
iterative an take O(n) and searching element in array take
O(logn) time
O(n)*O(logn)=O(nlogn)
T(overall)=2*(time for sorting) +time for finding and searching
T(overall)=2*O(nlogn)+O(nlogn)
T(overall)=3*O(nlogn)
T(overall)=O(nlogn)
Get Answers For Free
Most questions answered within 1 hours.