Double pointer || java || easy
Intuition\n1 Identify the Non-Decreasing Prefix\n\n- Traverse the array from the left to find the longest prefix that is sorted in non-decreasing order. Mark th
# Intuition\n1 Identify the Non-Decreasing Prefix\n\n- Traverse the array from the left to find the longest prefix that is sorted in non-decreasing order. Mark the end of this prefix as l.\n- If the entire array is sorted, return 0 as no removal is required.\n\n2 Identify the Non-Decreasing Suffix\n\n- Traverse the array from the right to find the longest suffix that is sorted in non-decreasing order. Mark the start of this suffix as r.\n\n3 Calculate Initial Candidate for Removal\n\n- The initial answer is the smaller of the two possible subarrays to remove:\n  - Remove all elements after the prefix:\uD835\uDC5B\u2212\uD835\uDC59\u22121\n- Remove all elements before the suffix: \uD835\uDC5F\n\n4 Merge the Prefix and Suffix\n\n- Use a two-pointer approach to try to merge parts of the prefix and suffix:\n  - One pointer (i) iterates over the prefix.\n  - Another pointer (j) iterates over the suffix.\n  - If merging at i and j keeps the array sorted, calculate the size of the removed subarray and update the minimum answer.\n  - \n5 Return the Result\n- The final value of the answer is the length of the shortest subarray to remove.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution{\npublic int findLengthOfShortestSubarray(int[] arr) {\n        int n=arr.length ;int l=0;int r=n-1;\n        while(l+1<n&&arr[l]<=arr[l+1]) ++l;\n        if(l==n-1){return 0;}\n        while(r>l&& arr[r-1]<=arr[r]) --r;\n        int ans=Math.min(n-l-1,r);\n        for(int i=0,j=r;i<=l && j<n;){\n            if(arr[i]<=arr[j]) ans=Math.min(ans,j-i++-1);\n            else ++j;\n        }\n        return ans;\n    }\n}\n```
['Java']