Given a unsorted sequence of a[1,…,n] of integers, give an O(nlogn) runtime algorithm to check there are two indices i and j such that a[i] =2*a[j]. The algorithm should return i=0 and j=2 on input 4,12,8,10 and false on input 4,3,1,11.
I think we have to sort the array anyways which is O(nlogn). I’m not sure what to do after that.
You’re right that the first step is sorting the array.
Once the array is sorted, you can find out whether a given element is inside the array in
O(log n)time. So if for every of thenelements, you check for the inclusion of another element inO(log n)time, you end up with a runtime ofO(n log n).Does that help you?