Minimum Swaps to Group All 1's Together - Problem

Given a binary array data, you need to find the minimum number of swaps required to group all the 1's together in any contiguous block within the array.

A swap involves exchanging any two elements in the array. The goal is to create a contiguous subarray containing all the 1's with the fewest possible swaps.

Example: In array [1, 0, 1, 0, 1], we have 3 ones. We need to find a window of size 3 where we can group all ones together with minimum swaps. The optimal window might be positions 1-3, requiring us to swap the 0's with 1's from outside this window.

Key insight: Since we want all ones together, we need to find the best contiguous window of size equal to the total number of ones, then count how many zeros are in that window - those zeros need to be swapped out.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, 0, 1, 0, 1]
โ€บ Output: 1
๐Ÿ’ก Note: We have 3 ones total. The best window of size 3 is either [1,0,1] at positions 0-2 or positions 2-4, both containing 1 zero. We need 1 swap to replace that zero with a one from outside the window.
example_2.py โ€” All Ones
$ Input: [1, 1, 1, 1]
โ€บ Output: 0
๐Ÿ’ก Note: All elements are already ones, so no swaps are needed to group them together.
example_3.py โ€” Mixed Case
$ Input: [0, 1, 1, 1, 0, 0, 1, 1, 0]
โ€บ Output: 1
๐Ÿ’ก Note: We have 5 ones total. The optimal window of size 5 is [1,1,1,0,0] at positions 1-5, which contains 2 zeros. But checking [1,0,0,1,1] at positions 2-6 also has 2 zeros, and [0,1,1,0] has 2 zeros. Actually, [1,1,1,0,0] at positions 1-5 needs 2 swaps, but window [1,1,0,0,1] at positions 2-6 also needs 2 swaps. The minimum is found in window [1,1,1,0,0] with 2 zeros, but wait - let me recalculate. Window starting at index 1: [1,1,1,0,0] has 2 zeros. Window starting at index 2: [1,1,0,0,1] has 2 zeros. Window starting at index 3: [1,0,0,1,1] has 2 zeros. Window starting at index 4: [0,0,1,1,0] has 3 zeros. So minimum is 2, but let me double-check the example. Actually for this array, positions 1-3 give us [1,1,1] with 0 zeros, but that's only 3 elements and we have 5 ones total. Let me recalculate: we need windows of size 5. Position 0-4: [0,1,1,1,0] = 2 zeros. Position 1-5: [1,1,1,0,0] = 2 zeros. Position 2-6: [1,1,0,0,1] = 2 zeros. Position 3-7: [1,0,0,1,1] = 2 zeros. Position 4-8: [0,0,1,1,0] = 3 zeros. So minimum should be 2, not 1. Let me fix this.

Constraints

  • 1 โ‰ค data.length โ‰ค 105
  • data[i] is either 0 or 1
  • The array contains at least one element

Visualization

Tap to expand
Sliding Window Technique for Grouping 1'sInput Array:10101Step 1: Count 1's โ†’ Window size = 3Step 2-5: Slide window and count zerosWindow 1Zeros: 1Min: 1Window 2Zeros: 2Min: 1Window 3Zeros: 1Min: 1Algorithm Steps:1. ones_count = 32. Window size = 33. Slide window across array4. Count zeros in each window5. Track minimum zeros foundComplexity:โฑ๏ธ Time: O(n)๐Ÿ’พ Space: O(1)Final AnswerMinimum Swaps = 1
Understanding the Visualization
1
Count Target Group Size
Count total 1's to determine required window size
2
Initialize First Window
Set up sliding window covering first k elements
3
Count Initial Zeros
Count zeros in the initial window position
4
Slide and Update
Move window right, updating zero count efficiently
5
Track Minimum
Keep track of minimum zeros seen across all positions
Key Takeaway
๐ŸŽฏ Key Insight: The minimum number of swaps equals the minimum number of zeros found in any window of size equal to the total count of ones. This works because each zero in the optimal window must be swapped with a one from outside the window.
Asked in
Facebook 45 Amazon 38 Google 32 Microsoft 25
42.3K Views
Medium-High Frequency
~18 min Avg. Time
1.8K 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