Time complexity O(nlogn) and space complexity O(n) in C++ accepted


#1

vector Solution::subUnsort(vector &A) {
int s=-1, e=-1, i;
vector ans;
int n=A.size();
vector B;
for(i=0; i<n; i++){
B.push_back(A[i]);
}
sort(B.begin(),B.end());
for(i=0; i<n; i++){
B[i]=B[i]-A[i];
}
for(i=0; i<n; i++){
if(B[i]!=0){
s=i;
break;
}
}
for(i=n-1; i>s; i–){
if(B[i]!=0){
e=i;
break;
}
}
if(s==-1){
ans.push_back(-1);
}else{
ans.push_back(s);
ans.push_back(e);
}
return ans;
}