Java Solution || Two Pointer || Binary Search
Hint : For this type of problem, drawing graph (or mountain ranges) by array values, and analyze that graph (mountains peak and valley), you will easily come up
**Hint** : For this type of problem, drawing graph (or mountain ranges) by array values, and analyze that graph (mountains peak and valley), you will easily come up with a solution.\n\n#### **Approach 1**\n**Time :** It will be far better than **O(Nlog(N))** {where N is length of array}, but I am not sure about the actual time-complexity.\n**Space :** O(1)\n```\nclass Solution {\n    public int binarySearch(int[] arr, int start, int val){\n        int res = arr.length;\n        int l = start;\n        int r = arr.length-1;\n        \n        while(l <= r){\n            int mid = l + (r-l)/2;\n            if( arr[mid] >= val){\n                res = mid;\n                r = mid-1;\n            }\n            else\n                l = mid+1;\n        }\n        \n        return res;\n    }\n    public int findLengthOfShortestSubarray(int[] arr) {\n        int n = arr.length;\n        int i = 0;\n        int j = n-1;\n\n        while(i+1 < n && arr[i] <= arr[i+1])\n            i++;\n        while(j-1 >= i && arr[j] >= arr[j-1])\n            j--;\n        \n        if( i == j)\n            return 0;\n        int max = Math.min(n-i-1, j);\n        \n        for(int k = 0; k <= i; k++){\n            int ind = binarySearch(arr, j, arr[k]);\n            max = Math.min(max, ind-k-1);\n        }\n        return max;\n    }\n}\n```\n#### **Approach 2**\n**Time :** **O(N)** {where N is length of array}\n**Space :** O(1)\n\n```\nclass Solution {\n    public int findLengthOfShortestSubarray(int[] arr) {\n        int n = arr.length;\n        int i = 0;\n        int j = n-1;\n\n        while(i+1 < n && arr[i] <= arr[i+1])\n            i++;\n        while(j-1 >= i && arr[j] >= arr[j-1])\n            j--;\n        \n        if( i == j)\n            return 0;\n        int min = Math.min(n-i-1, j);\n    \n        int left = 0;\n        \n        while(left <= i && j < n){\n            while( left <= i && arr[left] <= arr[j]){\n                left++;\n            }\n            min = Math.min(min, j-left);\n            j++;\n        }\n        return min;\n    }\n}\n```\n
[]