Solution
C++ []\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& A) {\n        sort(A.begin(), A.end());\n        int i = 0, n = A.size(), low =
```C++ []\nclass Solution {\npublic:\n    vector<int> numMovesStonesII(vector<int>& A) {\n        sort(A.begin(), A.end());\n        int i = 0, n = A.size(), low = n;\n        int high = max(A[n - 1] - n + 2 - A[1], A[n - 2] - A[0] - n + 2);\n        for (int j = 0; j < n; ++j) {\n            while (A[j] - A[i] >= n) ++i;\n            if (j - i + 1 == n - 1 && A[j] - A[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```\n\n```Python3 []\nclass Solution:\n    def numMovesStonesII(self, s: List[int]) -> List[int]:\n        s.sort()\n        n = len(s)\n        d1 = s[-2] - s[0] - n + 2\n        d2 = s[-1] - s[1] - n + 2\n        max_move = max(d1, d2)\n        if d1 == 0 or d2 == 0:\n            return [min(2, max_move), max_move]\n        max_cnt = left = 0\n        for right, x in enumerate(s):\n            while s[left] <= x - n:\n                left += 1\n            max_cnt = max(max_cnt, right - left + 1)\n        return [n - max_cnt, max_move]\n```\n\n```Java []\n\tclass Solution {\n\t\tpublic int[] numMovesStonesII(int[] stones) {\n\t\t\tint n = stones.length;\n\t\t\t int[] ans = new int[2];\n\t\t\t int i = 0, j = 0, wsize, scount, minMoves = Integer.MAX_VALUE;\n\t\t\tArrays.sort(stones);\n\t\t\t while (j < n) {\n\t\t\t\twsize = stones[j] - stones[i] + 1;\n\t\t\t\tscount = j - i + 1;\n\n\t\t\t\tif (wsize > n) {\n\t\t\t\t\ti++;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (wsize == n - 1 && scount == n - 1)\n\t\t\t\t\tminMoves = Math.min(minMoves, 2);\n\n\t\t\t\telse minMoves = Math.min(minMoves, n - scount);\n\n\t\t\t\tj++;\n\t\t\t}\n\t\t\tans[0] = minMoves;\n\t\t\tint maxMoves = 0;\n\t\t\t if (stones[1] == stones[0] + 1 || stones[n - 1] == stones[n - 2] + 1)\n\t\t\t\tmaxMoves = stones[n - 1] - stones[0] + 1 - n;\n\t\t\telse \n\t\t\t\tmaxMoves = Math.max(((stones[n - 1] - stones[1]) - (n - 1) + 1), ((stones[n - 2] - stones[0]) - (n - 1) + 1));\n\t\t\tans[1] = maxMoves;\n\t\t\treturn ans;\n\t\t}\n\t}\n```
['C++', 'Java', 'Python3']