Let A be an array with N entries. We consider continuous subarrays of it, these must be continuous , meaning they start at some index I and end on some index J. Denote m(I,J) the minimum element of the subarray starting at I and ending on J (contatining elements at index I and J inclusive). Find the maximum among m(I,J)*(J-I+1). J-I+1 is the subarray length.
Instead of running a loop over I and J and look for which has the minimum there should be a better algorithm in O(N) time. How can I do it? I am thinking about a thing like sliding window... First I pick the first element, I sucessively increase the width of the sliding window as long as the first element remains subarray minimum. That means that I will only have greater values. And I would eventually try to get rid of first element from sliding window to check whether greater elements lead to greater results. But how do I do it precisely?