Moving Stones Until Consecutive II || JAVASCRIPT || Solution by Bharadwaj
Approach\nSliding Window\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(1)\n\n# Code\n\nvar numMovesStonesII = function(stones) {\n  st
# Approach\nSliding Window\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nvar numMovesStonesII = function(stones) {\n  stones.sort((a, b) => a - b);\n  let { length: size } = stones;\n  let max = Math.max(stones[size - 1] - stones[1] - size + 2, stones[size - 2] - stones[0] - size + 2);\n  let start = 0;\n  let min = size;\n\n  for(let end = 1; end < size; end++){\n      while(stones[end] - stones[start] + 1 > size) start += 1;\n      let already = end - start + 1;\n\n      if(already === size - 1 && stones[end] - stones[start] + 1 === size - 1){\n          min = Math.min(min, 2);\n      } else {\n          min = Math.min(min, size - already);\n      }\n  }    \n  return [min, max];\n};\n```
['JavaScript']