🔥SIMPILL💯 % 🎯 |✨SUPER EASY BEGINNERS 👏
\n\n## Intuition\nThe problem asks us to find the shortest subarray that can be removed such that the remaining array is sorted in non-decreasing order. The key
\n\n## Intuition\nThe problem asks us to find the shortest subarray that can be removed such that the remaining array is sorted in non-decreasing order. The key insight is to consider the sorted left and right parts of the array and then determine how they can be connected.\n\n---\n\n## Approach\n1. Find the longest non-decreasing subarray from the left (`left`) and the longest non-decreasing subarray from the right (`right`).\n2. If the entire array is already sorted, return `0`.\n3. Use a two-pointer technique to determine the minimum length of the subarray that can be removed:\n   - One pointer starts at the end of the `left` subarray.\n   - Another pointer starts at the beginning of the `right` subarray.\n   - Check the possibility of connecting the `left` and `right` while minimizing the length of the subarray removed.\n4. Return the minimum length of the subarray to remove.\n\n---\n\n## Complexity\n- **Time Complexity**: \\(O(n)\\)  \n   Traversing the array to find `left` and `right` boundaries takes \\(O(n)\\). The two-pointer approach also takes \\(O(n)\\), resulting in an overall time complexity of \\(O(n)\\).\n- **Space Complexity**: \\(O(1)\\)  \n   The solution uses a constant amount of extra space.\n\n---\n\n\n```dart []\nclass Solution {\n  int findLengthOfShortestSubarray(List<int> arr) {\n    int n = arr.length;\n    int left = 0;\n\n    // Find the left sorted part\n    while (left < n - 1 && arr[left] <= arr[left + 1]) left++;\n    if (left == n - 1) return 0;\n\n    int right = n - 1;\n\n    // Find the right sorted part\n    while (right > 0 && arr[right - 1] <= arr[right]) right--;\n\n    // Case 1: Remove all elements between left and right\n    int result = min(n - left - 1, right);\n\n    // Case 2: Merge left and right parts\n    int start = 0, end = right;\n    while (start <= left && end < n) {\n      if (arr[start] <= arr[end]) {\n        result = min(result, end - start - 1);\n        start++;\n      } else {\n        end++;\n      }\n    }\n    return result;\n  }\n}\n```\n\n\n```java []\nclass Solution {\n    public int findLengthOfShortestSubarray(int[] arr) {\n        int n = arr.length;\n        int left = 0;\n\n        while (left < n - 1 && arr[left] <= arr[left + 1]) left++;\n        if (left == n - 1) return 0;\n\n        int right = n - 1;\n        while (right > 0 && arr[right - 1] <= arr[right]) right--;\n\n        int result = Math.min(n - left - 1, right);\n\n        int start = 0, end = right;\n        while (start <= left && end < n) {\n            if (arr[start] <= arr[end]) {\n                result = Math.min(result, end - start - 1);\n                start++;\n            } else {\n                end++;\n            }\n        }\n        return result;\n    }\n}\n```\n\n\n```javascript []\nvar findLengthOfShortestSubarray = function (arr) {\n    let n = arr.length;\n    let left = 0;\n\n    while (left < n - 1 && arr[left] <= arr[left + 1]) left++;\n    if (left === n - 1) return 0;\n\n    let right = n - 1;\n    while (right > 0 && arr[right - 1] <= arr[right]) right--;\n\n    let result = Math.min(n - left - 1, right);\n\n    let start = 0, end = right;\n    while (start <= left && end < n) {\n        if (arr[start] <= arr[end]) {\n            result = Math.min(result, end - start - 1);\n            start++;\n        } else {\n            end++;\n        }\n    }\n    return result;\n};\n```\n\n\n```python []\nclass Solution:\n    def findLengthOfShortestSubarray(self, arr):\n        n = len(arr)\n        left = 0\n\n        while left < n - 1 and arr[left] <= arr[left + 1]:\n            left += 1\n        if left == n - 1:\n            return 0\n\n        right = n - 1\n        while right > 0 and arr[right - 1] <= arr[right]:\n            right -= 1\n\n        result = min(n - left - 1, right)\n\n        start, end = 0, right\n        while start <= left and end < n:\n            if arr[start] <= arr[end]:\n                result = min(result, end - start - 1)\n                start += 1\n            else:\n                end += 1\n        return result\n```\n\n\n```cpp []\nclass Solution {\npublic:\n    int findLengthOfShortestSubarray(vector<int>& arr) {\n        int n = arr.size();\n        int left = 0;\n\n        while (left < n - 1 && arr[left] <= arr[left + 1]) left++;\n        if (left == n - 1) return 0;\n\n        int right = n - 1;\n        while (right > 0 && arr[right - 1] <= arr[right]) right--;\n\n        int result = min(n - left - 1, right);\n\n        int start = 0, end = right;\n        while (start <= left && end < n) {\n            if (arr[start] <= arr[end]) {\n                result = min(result, end - start - 1);\n                start++;\n            } else {\n                end++;\n            }\n        }\n        return result;\n    }\n};\n```\n\n\n```go []\nfunc findLengthOfShortestSubarray(arr []int) int {\n    n := len(arr)\n    left := 0\n\n    for left < n-1 && arr[left] <= arr[left+1] {\n        left++\n    }\n    if left == n-1 {\n        return 0\n    }\n\n    right := n - 1\n    for right > 0 && arr[right-1] <= arr[right] {\n        right--\n    }\n\n    result := min(n-left-1, right)\n\n    start, end := 0, right\n    for start <= left && end < n {\n        if arr[start] <= arr[end] {\n            result = min(result, end-start-1)\n            start++\n        } else {\n            end++\n        }\n    }\n    return result\n}\n\nfunc min(a, b int) int {\n    if a < b {\n        return a\n    }\n    return b\n}\n```\n\n![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style=\'width:250px\'}
['Array', 'Two Pointers', 'C', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'Dart']