Skip to content

Practice Minimum Adjacent Swaps for K Consecutive Ones

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1703
LeetCode Problem

Minimum Adjacent Swaps for K Consecutive Ones

Problem Statement

You are given an integer array, nums, and an integer k. nums comprises of only 0's and 1's. In one move, you can choose two adjacent indices and swap their values. Return the minimum number of moves required so that nums has k consecutive 1's.

Source: Minimum Adjacent Swaps for K Consecutive Ones on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,0,0,1,0,1], k = 2
Output
1

# Example 2

Input
nums = [1,0,0,0,0,0,1,1], k = 3
Output
5

# Example 3

Input
nums = [1,1,0,1], k = 2
Output
0

Constraints

1 <= nums.length <= 10^5
nums[i] is 0 or 1.
1 <= k <= sum(nums)

Related LeetCode Problems

Practice more problems similar to Minimum Adjacent Swaps for K Consecutive Ones.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.