Python Medium approach O(n)
```\ndef minMoves(self, nums: List[int], k: int) -> int:\n        p = [i for i, v in enumerate(nums) if v == 1]\n        # p[i]: the position of i-th 1\n       
```\ndef minMoves(self, nums: List[int], k: int) -> int:\n        p = [i for i, v in enumerate(nums) if v == 1]\n        # p[i]: the position of i-th 1\n        n = len(p)\n        #first sliding window sum\n        sm=0\n        for i in range(k):\n            sm+=abs(p[i]-p[k//2])\n        res=sm\n        i=0 \n        while i+k<n:\n            mid=i+k//2\n            sm-=abs(p[mid]-p[i])\n            sm+=abs(p[i+k]-p[mid+1])\n            sm+=(k//2)*(p[mid+1]-p[mid])\n            sm-=(k-1-k//2)*(p[mid+1]-p[mid])\n            res=min(res,sm)\n            i+=1\n        offset=0\n        for i  in range(k):\n            offset+=abs(i-k//2)\n        return res-offset\n
[]