Input: [2, 6, 4, 8, 10, 9, 15] Output: 5 Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
classSolution { public: intfindUnsortedSubarray(vector<int>& nums){ vector<int> clone_nums(nums); sort(clone_nums.begin(), clone_nums.end()); int l = 0, r = 0; for (int i = 0; i < clone_nums.size(); i++) { if (clone_nums[i] != nums[i]) { l = i; break; } } for (int i = clone_nums.size() - 1; i > 0; i--) { if (clone_nums[i] != nums[i]) { r = i; break; } } return r==l?0:r-l+1; } };