Shortest Subarray to be removed - Easy Solution - 🌟Beats 100%🌟
Intuition\nCount the continuous subarray that are increasing in the left from the start of the array and if the left equals the number of elements - 1, then the
# Intuition\nCount the continuous subarray that are increasing in the left from the start of the array and if the left equals the number of elements - 1, then the length of the subarray to be removed is 0.\n\nCount the continuous subarray that are increasing from the right in the right and using these find the length of subarray to be removed to make the array to be non-decreasing.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n**Initialization**:\n\nInitialize n to be the number of elements in the array.\n\nInitialize left to be 0 and right to be length of the elements minus 1.\n\n    int n=arr.length;\n    int left=0,right=arr.length-1;\n\n**Count the value of left**:\n\nIterate through the array till left is less than n and the value of left is less than the value of left+1 (increasing).\n\nIncrement the value of left in each iteration.\n\n    while(left<n-1&&arr[left]<=arr[left+1])\n        left++;\n\nIf left equals n - 1 , then no elements to be removed so the length of the subarray to be removed is 0.Hence return 0 immediately.\n\n    if(left==n-1) return 0;\n\n**Count the value of right**:\n\nIterate through the array from n-1 till the right becomes 0 and the element at the right is greater than or equal to the element at the right - 1.\n\nIncrement the value of right at each iteration.\n\n    while(right>0&&arr[right]>=arr[right-1])\n        right--;\n\n**Assign the value for res and i**:\n\nAssign the value of res with the minimum value of right and n-left-1 and to initialize i to be 0.\n\n    int res=Math.min(right,n-left-1);\n    int i=0;\n\n**Find length of the subarray to be removed**:\n\nIterate till i is less than or equal to left and till the right is less than n.\n\n    while(i<=left&&right<n){\n\nCheck if the value of element at index i is less than or equal to the element at the right,\n\nThen modify res as minimum value of res and right - i - 1 and increment the value of i at each iteration.\n\n    if(arr[i]<=arr[right])\n    {\n        res=Math.min(res,right-i-1);\n        i++;\n    }\n\nOtherwise, increment the value of right at each iteration.\n\n    else\n        right++;\n\n**Returning the value**:\n\nReturn the value of res which contains the length of the subarray to be removed to make the array to be non-decreasing.\n\n    return res;\n\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- ***Time complexity***: **O(n)**.\n    n -> number of elements in the array arr.\n\n**Initial Left Bound Calculation (left pointer)**: **O(n)**.\n\nThe while loop increments left as long as **arr[left] <= arr[left + 1]**.\n\nIn the worst case, this loop will iterate through the entire array, taking **O(n) time**.\n\n**Initial Right Bound Calculation (right pointer)**: **O(n)**.\n\nThe while loop decrements right as long as **arr[right] >= arr[right - 1]**.\n\nSimilarly, this loop may iterate through the entire array, taking\n**O(n)** time.\n\n**Main While Loop (Checking left and right boundaries)**: **O(n)**.\n\nThe outer while loop executes as long as **i <= left and right < n**.\n\nFor each valid i where **arr[i] <= arr[right]**, it calculates res and increments i. If **arr[i] > arr[right]**, it increments right.\n\nIn the worst case, this loop can **iterate up to O(n)** because each time either i or right moves, which together can traverse the entire array.\n\n**Overall Time Complexity**: **O(n)**.\n\nOverall, all operations are bounded by O(n) time complexity, so the total time complexity is:**O(n)**.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- ***Space complexity***: **O(1)**.\n\nThe algorithm uses only a few integer variables (n, left, right, res, and i).\n\n**No additional data structures** or storage are needed proportional to the input size n.\n\nHence the space complexity is : **O(1)**.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int findLengthOfShortestSubarray(int[] arr) {\n        int n=arr.length;\n        int left=0,right=arr.length-1;\n        while(left<n-1&&arr[left]<=arr[left+1])\n        left++;\n        if(left==n-1) return 0;\n        while(right>0&&arr[right]>=arr[right-1])\n        right--;\n        int res=Math.min(right,n-left-1);\n        int i=0;\n        while(i<=left&&right<n)\n        {\n            if(arr[i]<=arr[right])\n            {\n                res=Math.min(res,right-i-1);\n                i++;\n            }\n            else\n            right++;\n        }\n        return res;\n    }\n}\n```
['Array', 'Two Pointers', 'Java']