Sorted Array Approach in C++


#1
vector<int> Solution::subUnsort(vector<int> &A) {
vector<int> B(A.begin(),A.end());
sort(B.begin(),B.end());
int a=-1,b=-1;
int flag=0;
for(int i=0;i<A.size();i++){
    if(flag==0 && A[i]!=B[i]){
        flag=1;
        a=i;
    }
    if(flag==1 && A[i]!=B[i]){
        b=i;
    }
}
vector<int> ans;
// If the Array is already sorted.
if(a==-1){
ans.push_back(-1);
return ans;}

ans.push_back(a);
ans.push_back(b);
return ans;
}