C++ short solution
Run-time is O(NlogN), space is O(1). \n\n\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& stones) {\n        std::sort(stones.begin(),
Run-time is `O(NlogN)`, space is `O(1)`. \n\n```\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& stones) {\n        std::sort(stones.begin(), stones.end());\n        int size{(int)stones.size()}, maxSum{0};\n        for(int i{1}; i < size; ++i) maxSum += stones[i]-stones[i-1]-1;\n        maxSum -= std::min(stones[1]-stones[0]-1, stones[size-1]-stones[size-2]-1);\n        int len{0};\n        for(int i{0}, j{0}; j < size; ++j) {\n            while(stones[j]-stones[i] >= size) ++i;\n            bool add{stones[j]-stones[i] != size-1 && (i == 0 || j == size-1 && i < 2)};\n            len = std::max(len, j-i+1-add);\n        }\n        \n        return {size-len, maxSum};\n    }\n};\n```
[]