C++ O(NlogN) solution
\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& stones) {\n        sort(stones.begin(),stones.end());\n        int n = stones.size();
```\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& stones) {\n        sort(stones.begin(),stones.end());\n        int n = stones.size();\n        int minmove = n;\n        \n        auto i = stones.begin();\n        auto j = upper_bound(stones.begin(), stones.end(), stones[0]+n-1);\n        minmove = min (minmove, n - (int)(j - i));\n        j = stones.end();\n        i = lower_bound(stones.begin(), stones.end(), stones[n-1]-n+1);\n        minmove = min (minmove, n - (int)(j - i));\n        if (minmove == 1) {\n            if(stones.back()-stones.front() > n && (stones[n-1]- stones[1] == n -2 || stones[n-2]-stones[0] == n-2)) minmove++;\n        }\n        \n        //both side have outlier\n        for (int s = 1, t = 1; t < n; t++) {\n            if (stones[t] - stones[s] + 1 > n) {\n                minmove = min (minmove, n - (t - s) );\n                while (stones[t] - stones[s] + 1 > n)\n                    s++;\n            }\n        }\n        int maxmove=stones[n-1] - stones[0] + 1 - n;\n        if(stones[1]-stones[0]>1 && stones[n-1]-stones[n-2]>1) {\n            maxmove -= min(stones[1]-stones[0] - 1, stones[n-1]-stones[n-2] -1);\n        }\n        return vector{minmove,maxmove};\n    }\n};\n```\n
['C']