[C++], Lovely Binary Search, with comments
\nclass Solution {\npublic:\n    int findLengthOfShortestSubarray(vector<int>& arr) {\n        int n = arr.size(), idx1 = -1, idx2 = -1, prev = 0;\n        \n  
```\nclass Solution {\npublic:\n    int findLengthOfShortestSubarray(vector<int>& arr) {\n        int n = arr.size(), idx1 = -1, idx2 = -1, prev = 0;\n        \n        for(int i=0; i<n; i++) {\n            if(prev > arr[i]) {\n                idx1 = i - 1;\n                break;\n            }\n            prev = arr[i];\n        }\n        if(idx1 == -1) return 0;\n        \n        prev = 1e9;\n        for(int i=n-1; i>=0; i--) {\n            if(arr[i] > prev) {\n                idx2 = i + 1;\n                break;\n            }\n            prev = arr[i];\n        }\n        \n        // so arr parts [0, idx1] and [idx2, n - 1] are sorted;\n        int ans = n - 1;   // max #deletions needed\n        \n        ans = min(ans, n - idx1 - 1);     // deleting [idx1 + 1, n - 1]\n        ans = min(ans, idx2);           // deleting [0, idx2 - 1]\n        \n        // or we can pick elements from [0, idx1], and pick the least val x from [idx2, n - 1] at index j, \n        // such that arr[idx1] <= x, then we need to delete the middle part\n        \n        for(int i=0; i<=idx1; i++) {\n            int l = i + 1;   // #elements from left side\n            int r = arr.end() - lower_bound(arr.begin() + idx2, arr.end(), arr[i]);   //#elements from right side\n            \n            ans = min(ans, n - (l + r));   // left and right part, in combination is sorted, other rem part can be deleted\n        }\n        \n        return ans;\n    }\n};\n```
[]