Python solution (sliding window)
```\nclass Solution:\n    \n    # Helper function for calculating the cost 1st time\n    def calc_cost(self, ones_ls, start, end) :\n        mid = (start + end)
```\nclass Solution:\n    \n    # Helper function for calculating the cost 1st time\n    def calc_cost(self, ones_ls, start, end) :\n        mid = (start + end) // 2\n        tgt = ones_ls[mid]\n        left, right = mid - 1, mid + 1\n        cost = 0 \n        while left >= start :\n            cost += abs(ones_ls[left] - tgt)\n            left -= 1\n        while right <= end :\n            cost += abs(ones_ls[right] - tgt)\n            right += 1\n        \n        return cost\n        \n    \n    def minMoves(self, nums: List[int], k: int) -> int:\n        \n        # Storing all the indices at which \'1\' occurs\n        ones_ls = [i for i, j in enumerate(nums) if j]\n        \n        if len(ones_ls) < k :\n            return 0\n        \n        # Storing the difference between arr[i] - i.....In the range of consecutive ones, this value will be same\n        ones_ls = [(j - i) for i, j in enumerate(ones_ls)]\n        \n        ans = float("inf")\n        start, end = 0, k - 1\n        \n        # Our windows size will be equal to K\n        \n        # Calculating cost for initial window using helper function\n        ans = self.calc_cost(ones_ls, start, end)\n        cost = ans\n        prev_med = ones_ls[(start+end)//2]\n    \n        # Move the window 1 step towards right\n        start += 1\n        end += 1\n    \n        while end < len(ones_ls) :\n            mid = (start + end) // 2\n            new_med = ones_ls[mid]\n            \n            # Calculating the difference between current median and previous median \n            diff_med = new_med - prev_med\n            \n            # Now since our window has moved 1 step towards right, there will be some changes in the cost\n            \n            # Left part\n            # 1. Since the leftmost part of the previous window has been removed, subtract it\'s weightage from the cost\n            cost -= abs(prev_med - ones_ls[start-1])\n            \n            # 2. Balance the weightage with the difference between current median and previous median (since the median has been changed)\n            cost += diff_med * (mid - start)\n\n            \n            # Right part\n            # 1. Balance the weightage with the difference between current median and previous median (since the median has been changed) \n            cost -= diff_med * (end - mid)\n            \n            # 2. Since a new element has been added in the window, add it\'s weightage to the cost\n            cost += abs(ones_ls[end] - new_med)\n\n            \n            # Choose the minimum from previous minimum and current cost\n            ans = min(ans, cost)\n            \n            # Move window 1 step towards right\n            start += 1\n            end += 1\n            \n            # Current median becomes previous median for next iteration\n            prev_med = new_med\n            \n        return ans\n        \n        \n        \n        \n        \n        \n        \n        \n        \n
['Sliding Window', 'Python']