Monotonic Stack + Two-Pointers || O(n)
\n Add your space complexity here, e.g. O(n) \n\n# Code\njava []\n/*\nclass Solution {\n    public int findLengthOfShortestSubarray(int[] arr) {\n        // int
\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\n/*\nclass Solution {\n    public int findLengthOfShortestSubarray(int[] arr) {\n        // intuition I am getting is to why not we create a monotonic increasing stack \n        // and the elements which are not part of out stack are actual elements which prevent array from becoming increasing\n        Stack<Integer> stk = new Stack<>();\n        for(int i=0;i<arr.length;i++){\n            while(!stk.isEmpty() && stk.peek()>arr[i]){\n                stk.pop();\n            }\n            stk.push(arr[i]);\n        }\n        return arr.length-stk.size();\n    }\n}\nMonotonic stack doesn\'t standalone solve this problem, we need to think of ways which can remove minimum subarrray which cause array to become decreasing\nWrong Answer\n22 / 118 testcases passed\nInput\narr =\n[1,2,3,10,0,7,8,9]\n\nUse Testcase\nOutput\n4\nExpected\n2\n*/\nclass Solution {\n    public int findLengthOfShortestSubarray(int[] arr) {\n        int n=arr.length;\n        // We can use stack to create longest increasing  suffix array and then try to remove minimum subarray from prefix to make overall array increasing\n        Stack<Integer> stk = new Stack<>();\n        int right = n-1;\n        for(int i=n-1;i>=0;i--){\n            if(!stk.isEmpty() && arr[i]>stk.peek()){// 1 2 3 4 2 3 5 7 8 , index = 3\n                break;\n            }\n            stk.push(arr[i]);\n            right = i;\n        }\n        if(right == 0){\n            return 0;// array is already increasing, no need to remove\n        }\n        int minRemove = right;\n        int i = 0;\n        while(i<n && (i == 0 || arr[i-1]<=arr[i])){\n            /*\n            Initial state:\n                arr = [1, 2, 3, 10, 4, 2, 3, 5]\n                Prefix (0 to i):        [1, 2, 3, 10]\n                Suffix (right to end):       [2, 3, 5]\n                When i = 2:\n                arr[i] = 3, arr[right] = 2\n                3 > 2, so we need to increment right to find a point where arr[i] <= arr[right].\n                After right++, right = 6, and now arr[right] = 3.\n                Since arr[i] <= arr[right] (3 <= 3), we calculate minToRemove.\n            */\n            while(right<n && arr[i]>arr[right]){\n                //this loop ensures that we only merge parts of the array that maintain a non-decreasing order by adjusting right until arr[i] <= arr[right].\n                right++;\n            }\n            minRemove = Math.min(minRemove,right-i-1);\n            i++;\n        }\n        return minRemove;\n    }\n}\n/*\nSC: O(n) for stack\nTC: O(n)\n*/\n```
['Two Pointers', 'Monotonic Stack', 'Java']