C++ sliding window solution
\nclass Solution \n{\n    public:\n    vector<int> numMovesStonesII(vector<int>& a) \n    {\n        int n=a.size();\n        sort(a.begin(),a.end());\n        
```\nclass Solution \n{\n    public:\n    vector<int> numMovesStonesII(vector<int>& a) \n    {\n        int n=a.size();\n        sort(a.begin(),a.end());\n        int M=max(a[n-2]-a[0]-n+2,a[n-1]-a[1]-n+2);\n        int m=2147483647;\n        \n        int p1=0;\n        int p2=0;\n        while(true)\n        {\n            int last=a[p1]+n-1;\n            if(p1+1>=n&&p2+1>=n)\n            {\n                break;\n            }\n            else if(p2+1>=n)\n            {\n                if(a[p2]<=last)\n                {\n                    int s=p2-p1+1;\n                    \n                    if(a[p2]==last-1&&s==n-1)\n                    {\n                        m=min(m,n-s+1);\n                    }\n                    else\n                    {\n                        m=min(m,n-s);\n                    }\n                }\n                p1++;\n            }\n            else if(a[p2]<=last)\n            {\n                int s=p2-p1+1;\n                if(a[p2]==last-1&&s==n-1)\n                {\n                    m=min(m,n-s+1);\n                }\n                else\n                {\n                    m=min(m,n-s);\n                }\n                p2++;\n            }\n            else\n            {\n                p1++;\n            }\n        }\n        \n        return {m,M};\n    }\n};\n```
[]