Java Code
class Solution {\n    public int[] numMovesStonesII(int[] stones) {\n        Arrays.sort(stones);\n\n        int i=0, n=stones.length;\n        int high = Math.
class Solution {\n    public int[] numMovesStonesII(int[] stones) {\n        Arrays.sort(stones);\n\n        int i=0, n=stones.length;\n        int high = Math.max(stones[n-1] - n+2 -stones[1], stones[n-2]-stones[0]- n+2);\n\n        int low=n;\n        for(int j=0; j<n; j++){\n            while(stones[j]-stones[i] >= n) i++;\n\n            if(j-i+1 == n-1 && stones[j]-stones[i]==n-2) low = Math.min(low, 2);\n            else low = Math.min(low, n-(j-i+1));\n        }\n        return new int[]{low, high};\n    }\n}
[]