Skip to content

Instantly share code, notes, and snippets.

@nimbus98
Last active October 14, 2018 22:43
Show Gist options
  • Save nimbus98/dbb1f7800c596770cfe4c71c44cf680b to your computer and use it in GitHub Desktop.
Save nimbus98/dbb1f7800c596770cfe4c71c44cf680b to your computer and use it in GitHub Desktop.
IEEE CodeBuddy | Week 1 | Moksh Jain & Akash Nair
vector<int> Solution::subUnsort(vector<int> &A) {
int n = A.size();
if(A[0] > A[n-1]) {
vector<int> ret;
ret.push_back(0);
ret.push_back(n-1);
return ret;
}
vector<int> ret;
if(n == 1 || n == 0) {ret.push_back(-1); return ret;}
int flag = 0;
for(int i = 1; i < n; i++) if(A[i-1] > A[i]) {flag = 1; break;}
if(flag == 0) {ret.push_back(-1); return ret;}
int s = 0, e = n-1, i, max, min;
for (s = 0; s < n-1; s++)
{
if (A[s] > A[s+1])
break;
}
for(e = n - 1; e > 0; e--)
{
if(A[e] < A[e-1])
break;
}
max = A[s]; min = A[s];
for(i = s + 1; i <= e; i++){
if(A[i] > max)
max = A[i];
if(A[i] < min)
min = A[i];
}
for( i = 0; i < s; i++)
{
if(A[i] > min)
{
s = i;
break;
}
}
for( i = n -1; i >= e+1; i--)
{
if(A[i] < max)
{
e = i;
break;
}
}
ret.push_back(s);
ret.push_back(e);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment