public class Solution {
public int findUnsortedSubarray(int[] nums) {
int n = nums.length;
int[] temp = nums.clone();
Arrays.sort(temp);
int start = 0;
while (start < n && nums[start] == temp[start]) start++;
int end = n  1;
while (end > start && nums[end] == temp[end]) end;
return end  start + 1;
}
}
Java Solution, Sort.


@shawngao Thanks for the solution. These two lines can be replaced with
int[] temp = nums.clone();
int[] temp = new int[n]; for (int i = 0; i < n; i++) temp[i] = nums[i];


@shawngao test fails for input [1,2,3,4]
for that need to add extra condition as if(start==end)return 0;