The Indian Engineer

Problem 2134 Minimum Swaps to Group All 1s Together II

Posted on 7 mins

Array Sliding Window

Problem Statement

Link - Problem 2134

Question

A swap is defined as taking two distinct positions in an array and swapping the values in them.

A circular array is defined as an array where we consider the first element and the last element to be adjacent.

Given a binary circular array nums, return the minimum number of swaps required to group all 1's present in the array together at any location.

Example 1:

Input: nums = [0,1,0,1,1,0,0]
Output: 1
Explanation: Here are a few of the ways to group all the 1's together:
[0,0,1,1,1,0,0] using 1 swap.
[0,1,1,1,0,0,0] using 1 swap.
[1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).
There is no way to group all 1's together with 0 swaps.
Thus, the minimum number of swaps required is 1.

Example 2:

Input: nums = [0,1,1,1,0,0,1,1,0]
Output: 2
Explanation: Here are a few of the ways to group all the 1's together:
[1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).
[1,1,1,1,1,0,0,0,0] using 2 swaps.
There is no way to group all 1's together with 0 or 1 swaps.
Thus, the minimum number of swaps required is 2.

Example 3:

Input: nums = [1,1,0,0,1]
Output: 0
Explanation: All the 1's are already grouped together due to the circular property of the array.
Thus, the minimum number of swaps required is 0.

Constraints:

Solution

class Solution {
public:
    int minSwaps(vector<int>& nums) {
        int totOne = 0;
        int size = nums.size();
        for(auto & it:nums){
            if(it==1)
                totOne++;
        }
        for(int i = 0; i<totOne; i++)
            nums.push_back(nums[i]);


        int zeroCntr = 0;
        for(int i = 0; i<totOne; i++){
            if(nums[i]==0)
                zeroCntr++;
        }
        int minSwap = zeroCntr;
        for(int i=1;i<size;i++){
            if(nums[i-1]==0){
                zeroCntr--;
            }
            if(nums[i+totOne-1]==0){
                zeroCntr++;
            }
            minSwap = min(minSwap,zeroCntr);
        }
        return minSwap;


    }
};

Complexity Analysis

| Algorithm      | Time Complexity | Space Complexity |
| -------------- | --------------- | ---------------- |
| Sliding Window | O(n)            | O(n)             |

Explanation

Intial Thoughts

To approach this problem, consider the array as a circular list where the first and last elements are adjacent. We need to group all the 1’s together, so think of it like collecting all the 1’s in a single segment of the array. This can be achieved by minimizing the number of swaps required to bring all the 1’s together. We should start by identifying the total number of 1’s in the array and then try to find the segment that contains the most 1’s. This segment will be the starting point for our swaps.

Intuitive Analysis

Intuitively, solving this problem involves visualizing the array as a circle and identifying the minimum number of swaps needed to bring all the 1’s together. We can think of it like a sliding window approach, where we consider a window of size equal to the total number of 1’s in the array. We then slide this window over the array to find the position that requires the minimum number of swaps to bring all the 1’s together. The minimum number of swaps will be equal to the number of 0’s in this window. By considering all possible positions of the window, we can find the minimum number of swaps required.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Notice that the number of 1’s to be grouped together is fixed. It is the number of 1’s the whole array has.

Call this number total. We should then check for every subarray of size total (possibly wrapped around), how many swaps are required to have the subarray be all 1’s.

The number of swaps required is the number of 0’s in the subarray.

To eliminate the circular property of the array, we can append the original array to itself. Then, we check each subarray of length total.

How do we avoid recounting the number of 0’s in the subarray each time? The Sliding Window technique can help.


Similar Questions:

Title URL Difficulty
Minimum Swaps to Group All 1’s Together https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together Medium
Time Needed to Rearrange a Binary String https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string Medium