Simple O(n) time O(1) space cpp solution


#1
vector<int> Solution::subUnsort(vector<int> &A) {
int n = A.size();
int left = 0, right = 0, leftMax = A[0], rightMin = A[n - 1];

for (int i = 0; i < n; i++) 
    if (A[i] < leftMax) right = i;
    else leftMax = max(leftMax, A[i]);

for (int i = n - 1; i >= 0; i--) 
    if (A[i] > rightMin) left = i;
    else rightMin = min(rightMin, A[i]);


vector<int> reqArray;
if (left == right) reqArray.push_back(-1);
else {
    reqArray.push_back(left);
    reqArray.push_back(right);
}
return reqArray;
}