⭐ Easiest & Simple ✅ | Beats 100% 💯 | 0ms, O(N) | C++
Intuition\nThe Hint tab gives us idea hon how we can approach this problem. Since we need to find a subarray, it should be clear that we need to do something on
# Intuition\nThe `Hint` tab gives us idea hon how we can approach this problem. Since we need to find a `subarray`, it should be clear that we need to do something on the lines of prefix and suffix array. We will start by finding prefix and suffix array indexes and then try to minimize the no of element removal from the middle `subarray`\n\n# Approach\n1.\tTraverse from the left and right to find the longest sorted prefix and suffix.\n2.\tUse two pointers to merge the sorted prefix and suffix, and track the minimum subarray to be removed.\n3.\tReturn the length of the shortest subarray to remove.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int findLengthOfShortestSubarray(vector<int>& arr) {\n       \n        int n=arr.size(), i=0, j=n-1, ans=0;\n        while (i<n-1 && arr[i]<=arr[i + 1]){ //O(n) | Prefix Array\n            i++;\n        }\n\n        if (i==j) // If Already Sorted\n            return 0;\n\n        while (j>0 && arr[j-1]<=arr[j]){ // O(n) | Suffix Array\n            j--;\n        }\n\n        ans = min(j, n-i-1);\n        int left = 0, right = j;\n        while (left <= i && right < n) // O(n) | Merging Step\n        {\n            if (arr[left] <= arr[right]) {\n                ans = min(ans, right - left - 1); \n                left++; \n            } \n            else {\n                right++; \n            }\n        }\n\n        return ans; \n    }\n};\n```\n# Complexity\n- Time complexity:\n![Screenshot 2024-11-15 at 12.19.33.png](https://assets.leetcode.com/users/images/b26f162f-acab-4181-90da-4c0c7d8dd052_1731653529.997011.png)\n\n\n- Space complexity:\n![Screenshot 2024-11-15 at 12.19.44.png](https://assets.leetcode.com/users/images/dc6654ff-98b4-43a7-9026-33806adc8b29_1731653548.5921926.png)\n\n\n\n\n![Screenshot 2024-11-15 at 12.19.10.png](https://assets.leetcode.com/users/images/65d4bde2-af4f-4e57-94f4-3b19531f9e80_1731653502.4177806.png)\n\n\n\n
['Array', 'Two Pointers', 'Binary Search', 'C++']