Excellent edge cases 2 pointers C++ complete explanation
we have to remove smallest subarray after which, whole array will be sorted.\nfind l and r, \n0 to l is one sorted piece\nr to n-1 is other sorted piece\nwe hav
we have to remove smallest subarray after which, whole array will be sorted.\nfind l and r, \n0 to l is one sorted piece\nr to n-1 is other sorted piece\nwe have to keep some elements from 0 to l and check the smallest r that\'s suitable to hold the left piece. \nedge case1: we may remove all left elements, so answer is r(we have to remove all from 0 to r-1)\nedgecase2: we can remove all right ones(n-l+1)\nedgecase3: what if l==0 and r==n-1\nedgecase3.1: 6,5,4,3,2,1 a[0]<a[n-1], so i must remove i=1 to n-1\nEdgecase3.2:\n6,5,13,11 , we can remove all from i=1 to n-2\nso ans=min of all values\n```\nint lc1574(vector<int>v) {\nint n=v.size(),l=0, r=n-1,ans=n+1;\nwhile(l<n-1&&v[l]<=v[l+1])\n l++;\nwhile(r>l&&v[r]>=v[r-1]) \nr--; \nif(l==n-1) return 0;\nif(r==n-1 && l==0) {\n    if(v[l]<=v[r]) return n-2;\n    else return n-1; }\nint ml=l,mr=r,j=r;\nfor(int i=0;i<l+1;i++){\nwhile(r<n && v[i]>v[r]) r++;\nint tmpi=i;\nwhile(tmpi<l && v[tmpi]==v[tmpi+1]) tmpi++;\nans=min(ans,r-tmpi-1);\n}\n       return min({ans,mr,n-ml-1});\n\t \xA0\n }\n``
[]