We first sort the given array, and then we try to find the indices where values are different from original array. If the couldn't find it (`start == -1`

), then we return 0 cause the given array is already sorted. Otherwise, we return the length of difference (`end - start + 1`

).

```
public class Solution {
public int findUnsortedSubarray(int[] nums) {
int[] array = Arrays.copyOf(nums, nums.length);
Arrays.sort(array);
int start = -1, end = 0;
for(int i = 0; i < nums.length; i++) {
if(start == -1 && nums[i] != array[i]) start = i;
else if(start != -1 && nums[i] != array[i]) end = i;
}
return start == -1 ? 0 : end - start + 1;
}
}
```