Given an array of integers, write a method to find indices m and n such that if you sorted elements m through n, the entire array would be sorted. Minimize n - m (that is, find the smallest such sequence).
Return [m,n]. If there are no such m and n (e.g. the array is already sorted), return [-1, -1].
The shortest subarray that, once sorted, sorts the whole array. Sorting a copy and comparing ends works but uses linear extra space.
The right bound is the rightmost index smaller than some left-hand maximum; the left bound is the leftmost index larger than some right-hand minimum.
A left-to-right scan of \(mx\) updates \(right\); a right-to-left scan of \(mi\) updates \(left\). Two linear passes, constant extra space.
We first traverse the array \(array\) from left to right, and use \(mx\) to record the maximum value encountered so far. If the current value \(x\) is less than \(mx\), it means that \(x\) needs to be sorted, and we record the index \(i\) of \(x\) as \(right\); otherwise, update \(mx\).
Similarly, we traverse the array \(array\) from right to left, and use \(mi\) to record the minimum value encountered so far. If the current value \(x\) is greater than \(mi\), it means that \(x\) needs to be sorted, and we record the index \(i\) of \(x\) as \(left\); otherwise, update \(mi\).
Finally, return \([left, right]\).
The time complexity is \(O(n)\), where \(n\) is the length of the array \(array\). The space complexity is \(O(1)\).