Gather K Ones with Adjacent Swaps

Quick Overview

Gather K Ones with Adjacent Swaps. The binary array nums contains at least k ones. Each move swaps the entries at two adjacent indices. Return the fewest moves needed for the array to contain a block of k consecutive ones.

Gather K Ones with Adjacent Swaps

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem The binary array `nums` contains at least `k` ones. Each move swaps the entries at two adjacent indices. Return the fewest moves needed for the array to contain a block of k consecutive ones. ```cpp class Solution { public: int minMoves(vector<int>& nums, int k); }; ``` ### Examples **Example 1:** ```text nums = [1,0,1,0,1], k = 3 Output: 2 Explanation: Move the first one right once and the last one left once, forming [0,1,1,1,0]. ``` **Example 2:** ```text nums = [0,1,0,0,1,1], k = 2 Output: 0 Explanation: The final two positions already form the required block. ``` ### Constraints - `1 <= nums.length <= 10^5`. - Entries are 0 or 1. - `1 <= k <= sum(nums)`. ### Clarifying Questions Only adjacent swaps are legal, so a distant one cannot be moved in one operation. Other ones may remain outside the chosen block. ```hint Inspect the structure Which moves are legal, and must other ones outside the requested consecutive block disappear? ``` ### Reasoning checkpoints - Count adjacent swaps rather than unrestricted movements of chosen ones. - Prove the minimum cost for obtaining a block of `k` consecutive ones without imposing a destination block. ### Optional extensions Why do ordinary distances to the median overcount the spacing that the final block itself occupies?

Overview: Gather K Ones with Adjacent Swaps. The binary array nums contains at least k ones. Each move swaps the entries at two adjacent indices. Return the fewest moves needed for the array to contain a block of k consecutive ones.

Amazon logo
Amazon
Source window: 0–6 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The binary array nums contains at least k ones. Each move swaps the entries at two adjacent indices. Return the fewest moves needed for the array to contain a block of k consecutive ones.

class Solution {
public:
    int minMoves(vector<int>& nums, int k);
};

Examples

Example 1:

nums = [1,0,1,0,1], k = 3
Output: 2
Explanation: Move the first one right once and the last one left once, forming [0,1,1,1,0].

Example 2:

nums = [0,1,0,0,1,1], k = 2
Output: 0
Explanation: The final two positions already form the required block.

Constraints

  • 1 <= nums.length <= 10^5 .
  • Entries are 0 or 1.
  • 1 <= k <= sum(nums) .

Clarifying Questions Guidance

Only adjacent swaps are legal, so a distant one cannot be moved in one operation. Other ones may remain outside the chosen block.

Reasoning checkpoints

  • Count adjacent swaps rather than unrestricted movements of chosen ones.
  • Prove the minimum cost for obtaining a block of k consecutive ones without imposing a destination block.

Optional extensions

Why do ordinary distances to the median overcount the spacing that the final block itself occupies?