Created
March 23, 2018 13:56
-
-
Save exhesham/7b52f3c624b924a4c8c5325297981815 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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