Simple O(n) python solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n- Find all the ones and keep their indices as a list.\n- The total swaps 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- Find all the ones and keep their indices as a list.\n- The total swaps for the first k indices can be considered as the number of swaps (mid_steps) to move all indices to its mid point (mid=k//2) and then move back to consecutive indices (extra_steps).\n-- mid_steps = sum(indices[mid+k%2:k])-sum(indices[:mid])\n-- extra_tseps = (mid + k%2) * mid\n- Iterate over the rest of k indices to calculate each mid_steps as difference of 2 rolling sums.\n- return the minimum mid_steps - extra_steps\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution:\n    def minMoves(self, nums: List[int], k: int) -> int:\n        if k == 1 or len(nums) == k:\n            return 0\n\n        indices = [idx for idx, num in enumerate(nums) if num == 1]\n        mid = k // 2\n        # Move all k indices to indices[mid], and then back to consecutive positions.\n        # ex: [1,4,6,8,9] => [6,6,6,6,6] => [4,5,6,7,8]\n        #     (8+9)-(1+4)=12          2+1+0+1+2=(2+1)*2=6\n        # ex: [1,4,6,8] => [6,6,6,6] => [4,5,6,7]\n        #     (8)-(1+4)+6=9           2+1+0+1=(2+1)*2-2=2*2=4\n        odd = k % 2\n        extra_steps = (mid + odd) * mid\n        min_swaps = mid_steps = sum(indices[mid+odd:k])-sum(indices[:mid])\n        if mid_steps == extra_steps:\n            return 0\n\n        end = k\n        start = 0\n        for idx in range(mid, len(indices) - k + mid):\n            # 2 rolling sums\n            mid_steps += indices[start] - indices[idx] + indices[end] - indices[idx+odd]\n            start += 1\n            end += 1\n            if mid_steps == extra_steps:\n                return 0\n            elif min_swaps > mid_steps:\n                min_swaps = mid_steps\n\n        return min_swaps - extra_steps\n\n```
['Python3']