Easy & Simple Approach --> Shortest Subarray to be Removed to Make Array Sorted✅ || Beats 100 % 🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nThe approach identifies the longest sorted suffix at the end of the array and the longe
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe approach identifies the longest sorted suffix at the end of the array and the longest sorted prefix at the start. By trying to merge these two sorted parts, we determine the shortest subarray in the middle that can be removed to make the array sorted. We adjust pointers from both ends to minimize the removal length, ensuring the array remains non-decreasing.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tStep 1: Use a loop to determine the longest non-decreasing suffix, starting from the end and moving right leftward.\n2.\tStep 2: Initialize ans to the length of this suffix (right).\n3.\tStep 3: Expand left from the beginning, checking conditions for each left:\n\u2022\tWhile arr[left] > arr[right], increment right.\n\u2022\tUpdate ans to the minimum of ans and the current length of the subarray to remove (right - left - 1).\n1.\tReturn ans, which holds the shortest length of subarray required for removal.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n        O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n        O(1)\n# Code\n```cpp []\nclass Solution {\npublic:\n    int findLengthOfShortestSubarray(vector<int>& arr) {\n\n        int n = arr.size();\n        int left = 0, right = n - 1;\n        while(right > 0 && arr[right - 1] <= arr[right])\n        {\n            right--;\n        }\n        int ans = right;\n        while(left < right && (left == 0 || arr[left - 1] <= arr[left]))\n        {\n            while(right < n && arr[left] > arr[right])\n            {\n                right++;\n            }\n            ans = min(ans, right - left - 1);\n            left++;\n        }\n        return ans;\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        // Find longest non-decreasing suffix\n        while (right > 0 && arr[right - 1] <= arr[right]) {\n            right--;\n        }\n        \n        // If the entire array is already sorted\n        if (right == 0) return 0;\n        \n        int ans = right;\n        \n        // Find the minimum length of subarray to remove\n        while (left < right && (left == 0 || arr[left - 1] <= arr[left])) {\n            while (right < n && arr[left] > arr[right]) {\n                right++;\n            }\n            ans = Math.min(ans, right - left - 1);\n            left++;\n        }\n        \n        return ans;\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        # Find longest non-decreasing suffix\n        while right > 0 and arr[right - 1] <= arr[right]:\n            right -= 1\n        \n        # If the entire array is already sorted\n        if right == 0:\n            return 0\n        \n        ans = right\n        \n        # Find the minimum length of subarray to remove\n        while left < right and (left == 0 or arr[left - 1] <= arr[left]):\n            while right < n and arr[left] > arr[right]:\n                right += 1\n            ans = min(ans, right - left - 1)\n            left += 1\n        \n        return ans\n\n```\n```\n```\n![upvote.jpeg.jpg](https://assets.leetcode.com/users/images/1097516a-0073-4ae3-bd32-1e81917a63f0_1731647878.944862.jpeg)\n\n
['Binary Search', 'Python', 'C++', 'Java']