Is there a way to only return the min/max values of an array where the min is in a lower index position than the max?
For instance, let's say we have:
array = [10, 12, 5, 3, 20, 1, 9]
If we did .minmax on this array we would get the following:
array.minmax
=> [1, 20]
However, I'm only interested in getting the minimum value that has a lower index than the maximum value, in this case:
=> [3, 20]
I've been trying to think about different ways to do this but haven't had much luck. I tried to find a way to get the min/max values where the difference between the two returns a negative value but couldn't find a way to do that.
Edit: To clarify, this is essentially intended to be a stock picker. The array elements are the prices of a stock each day. I'm interested in maximizing the difference between a min/max in the array (buy on the day when stock = 3, sell on the day when stock = 15). The values may not be the absolute min or max.