Skip to content

Instantly share code, notes, and snippets.

@exhesham
Created March 23, 2018 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exhesham/7b52f3c624b924a4c8c5325297981815 to your computer and use it in GitHub Desktop.
Save exhesham/7b52f3c624b924a4c8c5325297981815 to your computer and use it in GitHub Desktop.
public int findUnsortedSubarray(int[] A) {
int n = A.length, beg = -1, end = -2, min = A[n-1], max = A[0];
for (int i=1;i<n;i++) {
max = Math.max(max, A[i]);
min = Math.min(min, A[n-1-i]);
if (A[i] < max) end = i;
if (A[n-1-i] > min) beg = n-1-i;
}
return end - beg + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment