Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Minimum Swaps to Group All 1's Together in Python
In a binary array, we often need to group all 1's together with minimum swaps. This problem uses a sliding window approach with prefix sums to find the optimal position for grouping all 1's.
Algorithm Approach
The key insight is to use a sliding window of size equal to the total count of 1's. We find the window position that already contains the maximum number of 1's, minimizing the swaps needed.
Steps
- Count total number of 1's in the array
- Create a prefix sum array for efficient range sum queries
- Use sliding window of size equal to count of 1's
- Find window with maximum 1's already present
- Minimum swaps = Total 1's − Maximum 1's in any window
Example
Let's implement the solution with a clear example −
def min_swaps_to_group_ones(data):
# Count total number of 1's
total_ones = sum(data)
n = len(data)
# If no 1's or all are 1's, no swaps needed
if total_ones == 0 or total_ones == n:
return 0
# Create prefix sum array
prefix_sum = [0] * n
prefix_sum[0] = data[0]
for i in range(1, n):
prefix_sum[i] = prefix_sum[i-1] + data[i]
# Find maximum 1's in any window of size total_ones
max_ones_in_window = 0
# Check all windows of size total_ones
for left in range(n - total_ones + 1):
right = left + total_ones - 1
# Count 1's in current window using prefix sum
if left == 0:
ones_in_window = prefix_sum[right]
else:
ones_in_window = prefix_sum[right] - prefix_sum[left - 1]
max_ones_in_window = max(max_ones_in_window, ones_in_window)
# Minimum swaps = Total 1's - Maximum 1's already in optimal window
return total_ones - max_ones_in_window
# Test the function
data = [1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1]
result = min_swaps_to_group_ones(data)
print(f"Array: {data}")
print(f"Minimum swaps needed: {result}")
Array: [1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1] Minimum swaps needed: 3
How It Works
For the array [1,0,1,0,1,0,0,1,1,0,1] −
- Total 1's = 6, so we need a window of size 6
- We check each possible window of size 6
- The optimal window contains 3 ones already
- Minimum swaps = 6 − 3 = 3
Optimized Solution
Here's a more space-efficient approach using sliding window without prefix sum −
def min_swaps_optimized(data):
total_ones = sum(data)
n = len(data)
if total_ones == 0 or total_ones == n:
return 0
# Count 1's in first window
current_ones = sum(data[:total_ones])
max_ones_in_window = current_ones
# Slide the window
for i in range(total_ones, n):
# Remove leftmost element, add rightmost element
current_ones = current_ones - data[i - total_ones] + data[i]
max_ones_in_window = max(max_ones_in_window, current_ones)
return total_ones - max_ones_in_window
# Test both approaches
data = [1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1]
print(f"Original approach: {min_swaps_to_group_ones(data)}")
print(f"Optimized approach: {min_swaps_optimized(data)}")
Original approach: 3 Optimized approach: 3
Time and Space Complexity
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Prefix Sum | O(n) | O(n) |
| Sliding Window | O(n) | O(1) |
Conclusion
The sliding window approach efficiently finds the minimum swaps needed to group all 1's together. The optimized version uses O(1) space while maintaining O(n) time complexity, making it ideal for large arrays.
