[JavaScript] Two Pointers + finding pivots, O(n) 2ms runtime, beats 100%, O(1) space
Intuition\n Describe your first thoughts on how to solve this problem. \nThe question allows us to remove 1 subarray, and we want a non-decreasing result, there
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe question allows us to remove 1 subarray, and we want a non-decreasing result, therefore we want to remove all possible strictly decreasing arrays. To do so we look for the first and last increasing subarrays and end on their pivot. The middle of the array would be excluded, marked as removed since it contains strictly decreasing subarrays.\nAfterwards we want to merge the two non-decreasing intervals we found to the minimal array to remove.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nI want to start at 1 from the left side and find the pivot where the array begins to decrease, that will be **i**. I will do the same with **j**, starting from the end at **arr.length - 1**, then work backwards until a decrease.\n\nOnce we find these 2 non-decreasing subarrays, each with the length of at least 1, we want to merge the 2 to find our longest possible non-decreasing subarray.\nIf we brute force the combinations it will be O(n^2), however since they are known to be in non-dereasing order we can have 2 pointers, on at 0, going to i, and pointer at j, going to n. Out pointers from 0 to i, p1, will track from what values always increment by 1 and p1 will track the number of values we want to include in this iteration starting from 0, while j to n, p2, will look for a value which is greater or equal to the value at the p1. Once we find the values we will know a valid possible solution to the answer, but we want to maximize the size of the remainding array, which in return minimizes the size of the subarray to remove. \n\nIt is possible that the array is strictly increasing or decreasing, so we will need to take that into account, therefore we calcuate the length of the non decreasing subarray from 0 to i and j to n and take the minimum of that to start. \n\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```javascript []\n/**\n * @param {number[]} arr\n * @return {number}\n */\nvar findLengthOfShortestSubarray = function(arr) {\n    const n = arr.length;\n    let i = 1;\n    while(i < n){\n        if(arr[i-1] <= arr[i]){\n            i++;\n        }else{\n            break;\n        }\n    }\n    let j = n-1;\n    while(j > i){\n        if(arr[j-1] <= arr[j]){\n            j--;\n        }else{\n            break;\n        }\n    }\n    let p1 = 0, p2 = j;\n    \n    let shortestSubArray = Math.min(n - i, n - (n - j));\n    while(p1 < i){\n        while(p2 < n && arr[p1] > arr[p2]){\n            p2++;\n        }\n        shortestSubArray = Math.min(shortestSubArray, n - (p1 + 1 + n - p2));\n        p1++;\n    }\n    \n    return shortestSubArray === -1 ? 0 : shortestSubArray;\n    \n};\n```
['Array', 'Two Pointers', 'JavaScript']