I am working on given an unsorted array with integer elements,
A={a_1,a_2,...,a_n}
finding largest difference between two elements in array (max|a_i-a_j|)
with using at most 3/2n
comparisons in the worst case.(Runtime do not matter and we can't use operations such as max or min).
I really doubt if this is possible: to find the maximum difference of two elements, in the worst case, shouldn't we always need about 2n
comparisons, as we need to use about n comparisons to find the largest element of the array and another n comparisons to find the smallest element of the array? I don't see where can I cut the operations.
I have also considered divide and conquer. Suppose I divide this array into 2 subarrays with length n/2
, but then I encountered the same problem, as finding maximum and minimum in each subarray with take about n
comparisons so there will be 2n
comparisons in total.
A hint on how to do this will be really appreciated.