Short C++ O(nlogn) solution, 7 lines
\nvector<int> numMovesStonesII(vector<int>& s) {\n    sort(s.begin(), s.end());\n    int n = s.size(), gaps = s[n - 1] - s[0] + 1 - n, low = INT_MAX;\n    if (s
```\nvector<int> numMovesStonesII(vector<int>& s) {\n    sort(s.begin(), s.end());\n    int n = s.size(), gaps = s[n - 1] - s[0] + 1 - n, low = INT_MAX;\n    if (s[1] - s[0] == gaps + 1 || s[n - 1] - s[n - 2] == gaps + 1)\n        low = min(2, gaps);\n    else for (int i = 0; i < n; i++) // window slides here\n        low = min<int>(low, n - (upper_bound(s.begin() + i, s.end(), s[i] + n - 1) - s.begin() - i));\n    return {low, max(s[n - 1] - s[1], s[n - 2] - s[0]) - n + 2};\n}\n```
[]