I am asking for your ideas regarding this problem:
I have one array A, with N elements of type double (or alternatively integer). I would like to find an algorithm with complexity less than O(N2) to find:
max A[i] - A[j]
For 1 < j <= i < n. Please notice that there is no abs(). I thought of:
- dynamic programming
- dichotomic method, divide and conquer
- some treatment after a sort keeping track of indices
Would you have some comments or ideas? Could you point at some good ref to train or make progress to solve such algorithm questions?
Make three sweeps through the array. First from
j=2up, filling an auxiliary arrayawith minimal element so far. Then, do the sweep from the topi=n-1down, filling (also from the top down) another auxiliary array,b, with maximal element so far (from the top). Now do the sweep of the both auxiliary arrays, looking for a maximal difference ofb[i]-a[i].That will be the answer.
O(n)in total. You could say it’s a dynamic programming algorithm.edit: As an optimization, you can eliminate the third sweep and the second array, and find the answer in the second sweep by maintaining two loop variables, max-so-far-from-the-top and max-difference.
As for “pointers” about how to solve such problems in general, you usually try some general methods just like you wrote – divide and conquer, memoization/dynamic programming, etc. First of all look closely at your problem and concepts involved. Here, it’s maximum/minimum. Take these concepts apart and see how these parts combine in the context of the problem, possibly changing order in which they’re calculated. Another one is looking for hidden order/symmetries in your problem.
Specifically, fixing an arbitrary inner point
kalong the list, this problem is reduced to finding the difference between the minimal element among alljs such that1<j<=k, and the maximal element amongis:k<=i<n. You see divide-and-conquer here, as well as taking apart the concepts of max/min (i.e. their progressive calculation), and the interaction between the parts. The hidden order is revealed (kgoes along the array), and memoization helps save the interim results for max/min values.The fixing of arbitrary point
kcould be seen as solving a smaller sub-problem first (“for a givenk…”), and seeing whether there is anything special about it and it can be abolished – generalized – abstracted over.There is a technique of trying to formulate and solve a bigger problem first, such that an original problem is a part of this bigger one. Here, we think of find all the differences for each k, and then finding the maximal one from them.
The double use for interim results (used both in comparison for specific
kpoint, and in calculating the next interim result each in its direction) usually mean some considerable savings. So,