Easy and simple Solution |✅✅Beats 100% | Prefix + Suffix | C++ || Java | Python | Javascript
Upvote if it helps  \n\n{:height="30px" width="30px"} \n\nConnect with me on Bijoy Sing !\n---\n\ncpp []\nclass Solution {\npublic:\n    int findLengthOfShortes
# Upvote if it helps  \n![image.png](https://assets.leetcode.com/users/images/24d5fdff-e842-4b78-b01b-469f26a60636_1730866877.2707105.png)\n{:height="30px" width="30px"} \n\nConnect with me on [Bijoy Sing ](https://www.linkedin.com/in/bijoy-sing-236a5a1b2/)!\n---\n\n```cpp []\nclass Solution {\npublic:\n    int findLengthOfShortestSubarray(vector<int>& arr) {\n        int n = arr.size();\n        int left = 0;\n        int right = n - 1;\n\n        while (left < n - 1 && arr[left] <= arr[left + 1]) {\n            left++;\n        }\n        if (left == n - 1)\n            return 0;\n\n        while (right > 0 && arr[right] >= arr[right - 1]) {\n            right--;\n        }\n        int result = min(n - left - 1, right);\n        \n        int i = 0, j = right;\n        while (i <= left && j < n) {\n            if (arr[i] <= arr[j]) {\n                result = min(result, j - i - 1);\n                i++;\n            } else {\n                j++;\n            }\n        }\n        return result;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def findLengthOfShortestSubarray(self, arr):\n        n = len(arr)\n        left, right = 0, n - 1\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        while right > 0 and arr[right] >= arr[right - 1]:\n            right -= 1\n        result = min(n - left - 1, right)\n        \n        i, j = 0, right\n        while i <= left and j < n:\n            if arr[i] <= arr[j]:\n                result = min(result, j - i - 1)\n                i += 1\n            else:\n                j += 1\n        return result\n```\n\n```javascript []\nclass Solution {\n    findLengthOfShortestSubarray(arr) {\n        const n = arr.length;\n        let left = 0, right = n - 1;\n\n        while (left < n - 1 && arr[left] <= arr[left + 1]) {\n            left++;\n        }\n        if (left === n - 1) return 0;\n\n        while (right > 0 && arr[right] >= arr[right - 1]) {\n            right--;\n        }\n        let result = Math.min(n - left - 1, right);\n        \n        let i = 0, j = right;\n        while (i <= left && j < n) {\n            if (arr[i] <= arr[j]) {\n                result = Math.min(result, j - i - 1);\n                i++;\n            } else {\n                j++;\n            }\n        }\n        return result;\n    }\n}\n```\n\n```java []\nclass Solution {\n    public int findLengthOfShortestSubarray(int[] arr) {\n        int n = arr.length;\n        int left = 0, right = n - 1;\n\n        while (left < n - 1 && arr[left] <= arr[left + 1]) {\n            left++;\n        }\n        if (left == n - 1) return 0;\n\n        while (right > 0 && arr[right] >= arr[right - 1]) {\n            right--;\n        }\n        int result = Math.min(n - left - 1, right);\n        \n        int i = 0, j = right;\n        while (i <= left && j < n) {\n            if (arr[i] <= arr[j]) {\n                result = Math.min(result, j - i - 1);\n                i++;\n            } else {\n                j++;\n            }\n        }\n        return result;\n    }\n}\n```\n\n### Problem Summary\nWe have an array of numbers, and we want to remove the smallest possible section of the array to make the remaining numbers sorted in non-decreasing order. The section we remove must be a continuous part of the array.\n\n### Steps to Solve the Problem\n\n1. **Look for the Sorted Parts**:\n   - First, we check the beginning of the array to see how far it is already sorted in increasing order. We\u2019ll call this the **prefix**.\n   - Then, we check from the end of the array backwards to find the longest sorted part at the end, called the **suffix**.\n\n2. **If Already Sorted**:\n   - If the whole array is already sorted in increasing order, we don\u2019t need to remove anything. In this case, the answer is `0`.\n\n3. **Calculate Possible Removals**:\n   - If the array is not fully sorted, we can try removing either:\n     - Everything after the prefix (from the end of the prefix to the end of the array).\n     - Everything before the suffix (from the start of the array up to the beginning of the suffix).\n   - We start with the smaller of these two options.\n\n4. **Try to Merge the Sorted Parts**:\n   - Now, we want to see if we can combine part of the prefix and part of the suffix to form a larger sorted section in the middle. \n   - We move pointers from the end of the prefix and the beginning of the suffix to find if we can connect these two sorted sections without breaking the order.\n\n5. **Find the Smallest Removal**:\n   - By connecting these sorted sections, we can see the smallest section we actually need to remove to make the array fully sorted.\n\n### Example\nImagine the array `[1, 2, 3, 10, 4, 2, 3, 5]`:\n- The prefix `[1, 2, 3, 10]` is sorted, and the suffix `[2, 3, 5]` is sorted.\n- Removing everything between these two sections might be the shortest way to sort the array.\n### *If you have any questions about this approach or need further clarification, feel free to drop a comment!*\n
['Array', 'Two Pointers', 'Sorting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']