Suppose that I have an array of random reals, how do I the indices pair (i, j) where j<i and
maximise (a[j] - a[i]) in O(n ln(n)) time
n ln n sugguest recursions. So I was thinking of sorting the array in n ln n sort. but then the min and mix of the sorted array might not satisfy j<i
The difference a[j]-a[i] depends on all i , j so scanning all the possible permutation is O(n^2). Does anybody have a suggestion on how I can divide the problem space?
If I understand you correctly, you want to find
max(a[j] - a[i])among all pairs(i, j)wherej < i.You can do it in O(n) without much problem.
For each index
i, to maximizea[j] - a[i]expression we need to findmax(a[j])on interval[0 .. i - 1]. So, let’s move from left to right (increasei) and keep current maximum value ofa[j].