Minimum Moves to Pick K Ones - Problem

You're given a binary array nums of length n, and two integers: k (the number of ones to collect) and maxChanges (maximum flips allowed).

Alice's Mission: Collect exactly k ones using the minimum number of moves possible!

Game Rules:

  1. Alice starts at any index aliceIndex she chooses
  2. If there's a 1 at her starting position, she picks it up for free (no move cost)
  3. She can perform two types of moves:
    • Create a 1: Change any 0 to 1 (costs 1 move, limited to maxChanges times)
    • Swap adjacent: Swap a 1 with an adjacent 0 (costs 1 move). If the 1 moves to Alice's position, she picks it up

Goal: Return the minimum number of moves needed to collect exactly k ones.

Example: With nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1, Alice could start at index 5, pick up that 1 for free, create a 1 at index 4 (1 move), then swap it to her position (1 move), and swap the 1 from index 6 to her position (1 move). Total: 3 moves for 3 ones.

Input & Output

example_1.py — Basic Case
$ Input: nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1
Output: 3
💡 Note: Alice can start at index 5 (has a 1), create a 1 at index 4 using maxChanges (1 move), swap it to position 5 (1 move), then swap the 1 from index 6 to position 5 (1 move). Total: 3 moves to collect 3 ones.
example_2.py — Use MaxChanges Optimally
$ Input: nums = [0,0,0,0], k = 2, maxChanges = 3
Output: 4
💡 Note: No existing ones, so Alice must create all ones using maxChanges. She can start anywhere, create a 1 at her position (1 move), pick it up (1 move), create another 1 (1 move), pick it up (1 move). Total: 4 moves for 2 ones.
example_3.py — Adjacent Ones Advantage
$ Input: nums = [1,1,1,1,1], k = 3, maxChanges = 0
Output: 2
💡 Note: Alice should start at index 2 (middle). She gets 1 one for free at her position, then can swap adjacent ones at indices 1 and 3 to her position (1 move each). Total: 2 moves for 3 ones.

Constraints

  • 1 ≤ nums.length ≤ 105
  • nums[i] is either 0 or 1
  • 1 ≤ k ≤ nums.length
  • 0 ≤ maxChanges ≤ 109
  • The answer is guaranteed to exist

Visualization

Tap to expand
Minimum Moves to Pick K Ones INPUT Binary Array nums[] 1 1 0 0 0 1 1 0 0 1 0 1 2 3 4 5 6 7 8 9 Parameters k = 3 (ones to collect) maxChanges = 1 (flips) Legend = One (1) = Zero (0) Total 1s in array: 5 Need to collect: 3 ALGORITHM STEPS (Greedy Approach) 1 Choose Start Position Pick index 5 or 6 (has 1s) Alice starts at index 5 2 Pick Free One Collect 1 at index 5 FREE Collected: 1, Moves: 0 3 Use maxChanges Create 1 at index 4: 1 move Swap to Alice: 1 move Collected: 2, Moves: 2 4 Swap Adjacent One Swap 1 at index 6 to 5 Cost: 1 move Collected: 3, Moves: 3 Strategy Summary: 1 free + 2 change + 1 swap FINAL RESULT Minimum Moves 3 Move Breakdown 1. Start at index 5 Pick 1 FREE (0 moves) 2. Create + Swap 2 moves (used maxChanges) 3. Swap from index 6 1 move OK - VERIFIED Collected 3 ones optimally! Key Insight: The greedy strategy prioritizes: (1) Free pickup at start position, (2) Using maxChanges efficiently by creating adjacent 1s that cost 2 moves (create + swap), (3) Swapping existing 1s which cost 1 move each. Starting at a position with consecutive 1s minimizes total distance traveled and thus the total number of swap operations needed. TutorialsPoint - Minimum Moves to Pick K Ones | Greedy Approach
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
26.0K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen