O(n) time accepted solution


#1

vector Solution::subUnsort(vector &A) {
vector B = A;
int p1 = -1;
int p2 = -1;
int min_ = INT_MAX;
bool flag = true;
for( int i = 0; i < A.size()-1; i++)
{
if (A[i] > A[i+1] && p1 == -1)
{
flag = false;
p1 = i;
p2 = i+1;
min_ = min(min_, A[p2]);
swap(A[i], A[i+1]);

    }
    else if(A[i] > A[i+1])
    {
        p2 = i+1;
        min_ = min(min_, A[p2]);
        swap(A[i], A[i+1]);
    }
}  
vector<int> sub;
if(flag)
{
    sub.push_back(-1);
    return sub;
}
for (int i = 0; i< B.size(); i++)
{
    if(B[i] > min_ )
    {
        p1 = i;
        break;
    }
}
sub.push_back(p1);
sub.push_back(p2);
return sub;

}

With some tweaks, it can be made O(1) space solution. Right now, it is O(n) space solution.