Python | Sliding Window Solution
maintan a window that contains k 1s and update the minimum swap by subtracting the swap of first 1 in the window and adding the swap of the new 1. When n is eve
maintan a window that contains k 1s and update the minimum swap by subtracting the swap of first 1 in the window and adding the swap of the new 1. When n is even, the overlapping part has odd number of 1s and the rightmost one is not countered by symmetry so we need to subtract one more time.\n\n```\nclass Solution:\n    def minMoves(self, nums: List[int], k: int) -> int:\n        n = len(nums)\n        if k == 1:\n            return 0\n        ones = [i for i in range(n) if nums[i] == 1]\n        if k == 2:\n            return min([ones[i] - ones[i-1] for i in range(1, len(ones))]) - 1\n        left = 0\n        right = k - 1\n        middle = (k - 1) // 2\n        swap = 0\n        for i in range(0, k):\n            swap += abs(ones[i] - (ones[middle] + i - middle))\n        min_swap = swap\n        \n        while right < len(ones) - 1:\n            swap -= (ones[middle] - (middle - left) - ones[left])\n            left += 1\n            right += 1\n            middle += 1\n            swap += ones[right] - (ones[middle] + right - middle)\n            if k % 2 == 0:\n                swap -= (ones[middle] - ones[middle - 1] - 1)\n            min_swap = min(min_swap, swap)\n        return min_swap\n```
['Python']