1703. Minimum Adjacent Swaps for K Consecutive Ones
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def minMoves(self, nums: List[int], k: int) -> int:
        positions = [idx for idx, value in enumerate(nums) if value == 1]
        total_ones = len(positions)
        cumulative_sum = [0] * (total_ones + 1)

        for i in range(total_ones):
            cumulative_sum[i + 1] = cumulative_sum[i] + positions[i]

        optimal_moves = float('inf')

        if k % 2 == 1:
            half_window = (k - 1) // 2
            for i in range(half_window, total_ones - half_window):
                right_cost = cumulative_sum[i + half_window + 1] - cumulative_sum[i + 1]
                left_cost = cumulative_sum[i] - cumulative_sum[i - half_window]
                optimal_moves = min(optimal_moves, right_cost - left_cost)
            return optimal_moves - half_window * (half_window + 1)

        else:
            half_window = (k - 2) // 2
            for i in range(half_window, total_ones - half_window - 1):
                right_cost = cumulative_sum[i + half_window + 2] - cumulative_sum[i + 1]
                left_cost = cumulative_sum[i] - cumulative_sum[i - half_window]
                optimal_moves = min(optimal_moves, right_cost - left_cost - positions[i])
            return optimal_moves - half_window * (half_window + 1) - (half_window + 1)

```
['Python3']