Given an array A of integers, find the maximum of j - i such that A[i] <= A[j]. If there is no solution possible, return -1.
The Analysis
The brute-force solution is simple with two loops nested. However, we are looking for a better solution. A O(nlog n) solution should come in mind after an O(n^2) solution. An O(n log n) approach is usually searching on a sorted array. But we need to have at least a sorted array first. How?
We apply a frontier approach technique from both ends. We create an array to keep the minimum element (including the current one) from left to right. We create another array to keep the maximum element (including the current one) from right to left. Thus the first array is non-increasing from the left and the second array is non-decreasing from the right. Let’s call the first array minLeft and the second array maxRight.
Then, for each element in the maxRight from right to left, we need to find the first position in minLeft that is smaller than the element. Then we update the distance. (If it helps, we can visualize two non-increasing curves running from left to right - one curve will be above the other where x-axis is the index and y-axis is the value).
Because the the minLeft is non-increasing, thus when we apply a binary search technique, we should watch out for the sides we perform the next search.