1040. Moving Stones Until Consecutive II.cpp
Code\n\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& stones) {\n        sort(stones.begin(), stones.end());\n        int n = stones.
**Code**\n```\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& stones) {\n        sort(stones.begin(), stones.end());\n        int n = stones.size(), i = 0, low = n;\n        int high = max(stones[n-1] - n + 2 - stones[1], stones[n-2] - stones[0] - n + 2);\n        for (int j = 0; j < n; ++j) {\n            while (stones[j] - stones[i] >= n) ++i;\n            if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2)\n                low = min(low, 2);\n            else\n                low = min(low, n - (j - i + 1));\n        }\n        return {low, high};\n    }\n};\n```
['C']