Not shortest but easiest to understand solution
\nclass Solution {    \n    public int[] numMovesStonesII(int[] stones) {\n      int max;\n      Arrays.sort(stones);\n      int len = stones.length;\n      int
```\nclass Solution {    \n    public int[] numMovesStonesII(int[] stones) {\n      int max;\n      Arrays.sort(stones);\n      int len = stones.length;\n      int gaps = stones[len - 1] - stones[0] + 1 - len;\n      if (stones[0] + 1 == stones[1] || stones[len - 1] == stones[len - 2] + 1) {\n        max = gaps;\n      } else {\n        int minE = Math.min(stones[1] - stones[0] - 1, stones[len - 1] - stones[len - 2] - 1);\n        max = gaps - minE;\n      }\n      \n      int cnt = 0;\n      int b = 0;\n      int sc = 0;\n      int min = Integer.MAX_VALUE;\n      while (true) {\n        int lst = stones[b] + len - 1;\n        if (lst > stones[len - 1]) {\n          break;\n        }\n        while (true) {\n          if (sc < len && stones[sc] <= lst) {\n            cnt++;\n            sc++;\n          } else {\n            break;\n          }\n        }\n        gaps = len - cnt;\n        if (!(b == 0 && gaps == 1 && sc == len - 1)) {\n          min = Math.min(min, gaps);\n        }\n        \n        b++;\n        cnt--;\n      }\n      \n      int fst = stones[len - 1] - (len - 1);\n      sc = len - 1;\n      cnt = 0;\n      while (sc >= 0 && stones[sc] >= fst) {\n        cnt++;\n        sc--;\n      }\n      \n      gaps = len - cnt;\n      if (!(gaps == 1 && sc == 0)) {\n        min = Math.min(min, len - cnt);\n      }\n      int[] res = new int[2];\n      res[0] = min;\n      res[1] = max;\n      return res;\n    }\n}\n\n```
[]