O(n) sliding window solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def minMoves(self, nums: List[int], k: int) -> int:\n        if k==1:\n            return 0\n        ones_inds = [i for i in range(len(nums)) if nums[i]==1]\n        if k == 2:\n            return min([ones_inds[i]-ones_inds[i-1]-1 for i in range(1, len(ones_inds))])\n        left_count = 0\n        right_count = 0\n        count_arr = [-1]*(k-1)\n        left_swaps = 0\n        right_swaps = 0\n        for i in range(0,len(count_arr)):\n            count_arr[i] = ones_inds[i+1]-ones_inds[i]-1\n            steps = min(i+1,k-1-i)\n            if steps == k-1-i:\n                right_swaps +=steps*count_arr[i]\n            else:\n                left_swaps += steps*count_arr[i]\n\n\n        total_swaps = left_swaps + right_swaps\n        mid_int = int((k-1)/2)\n        left_count = sum(count_arr[0:mid_int])\n        right_count = sum(count_arr[mid_int:k-1])  \n        min_swaps = total_swaps    \n        for i in range(1,len(ones_inds)-(k-1)):\n            left_swaps -= left_count\n            middle_count = ones_inds[mid_int+1]-ones_inds[mid_int]-1\n            left_swaps += middle_count*int((k-1)/2)           \n            left_count += middle_count\n            left_count -=( ones_inds[i]-ones_inds[i-1]-1)\n            right_count -= middle_count\n            right_swaps -= middle_count*int(k/2)\n            right_swaps+= right_count\n            added_count= (ones_inds[i+k-1]-ones_inds[i+k-2]-1)\n            right_count += added_count\n            right_swaps += added_count\n            total_swaps = left_swaps + right_swaps\n            min_swaps = min(min_swaps, total_swaps)\n            mid_int += 1\n\n        return min_swaps\n\n\n\n\n```
['Python3']