# Solution For Minimum Adjacent Swaps For K Consecutive Ones

Problem Description:

Given an array of zeros and ones, find the minimum number of adjacent swaps required to group K consecutive ones together.

Example:

Input: nums = [1,0,1,0,1], k = 2

Output: 0

Explanation: We can group the 1s together as [1,1,0,1,0] or [0,1,1,0,1]. No swaps are needed.

Approach:

We can solve this problem by using a sliding window approach. The window size will be K, and we will keep track of the number of ones in the window. We will start by traversing the array and count the number of ones in the first window. Then we will move the window one step at a time and calculate the number of ones in the new window. We will also keep track of the minimum number of swaps required to group all the ones in the window together.

When we move the window, we will subtract the number of ones that are leaving the window and add the number of ones that are entering the window. If the new window has all ones grouped together, we do not need to do any swaps. Otherwise, we need to calculate the number of swaps required to group the ones together.

The number of swaps required is equal to the number of ones in the window minus the number of ones grouped together. To group the ones together, we need to find the middle point of the window and swap the ones that are on the left side of the middle point with the ones on the right side of the middle point.

We will keep track of the minimum number of swaps required to group all the ones in each window together. After traversing the entire array, we will return the minimum number of swaps required.

Algorithm:

- Initialize variables countOnes = 0 and minSwap = infinity.
- Traverse the first window of size K and count the number of ones in it.
- Traverse the array from index K to N-1.
- In each iteration, if the first element in the current window is one, subtract one from countOnes.
- If the last element in the current window is one, add one to countOnes.
- If countOnes == K, the window has all ones grouped together, and we do not need to do any swaps. Otherwise, calculate the number of swaps required to group the ones together.
- If the number of swaps required is less than minSwap, update minSwap.
- Return minSwap.

Code:

Time Complexity: O(N).

## Step by Step Implementation For Minimum Adjacent Swaps For K Consecutive Ones

class Solution { public int minAdjSwaps(int[] A, int k) { int n = A.length, res = 0; for (int i = 0; i < n; ++i) if (A[i] == 1) { int j = i; while (j < n && A[j] == 1) ++j; res += (j - i - k) / 2; } return res; } } /* Explanation: We iterate through the array, and whenever we encounter a "1", we check if there are at least "k" consecutive "1"s afterwards. If there are, we update the result variable accordingly. For example, consider the array [1,0,1,1,0,1,1,1,1,1] and k = 2. We iterate through the array and encounter the first "1" at index 0. We then check if there are at least 2 more "1"s afterwards. In this case, there are 5 more "1"s afterwards, so we update res to res + 5 - 2 = 3. We then encounter the next "1" at index 3. We then check if there are at least 2 more "1"s afterwards. In this case, there are 2 more "1"s afterwards, so we update res to res + 2 - 2 = 2. We then encounter the next "1" at index 5. We then check if there are at least 2 more "1"s afterwards. In this case, there are 0 more "1"s afterwards, so we update res to res + 0 - 2 = 0. We then encounter the next "1" at index 7. We then check if there are at least 2 more "1"s afterwards. In this case, there are 0 more "1"s afterwards, so we update res to res + 0 - 2 = 0. At the end, res = 2.

def minAdjSwaps(arr, k): # This function returns the minimum number of swaps # required to convert the given binary array into # a string of k consecutive 1's # n is the length of the array n = len(arr) # Variable to store the maximum number of # consecutive 1's found so far max_ones = 0 # Variable to store the index of the # leftmost element having maximum number # of consecutive 1's left_index = 0 # Traverse the given array for i in range(n): # If current element is 0 if (arr[i] == 0): # Find the number of consecutive 1's # including this 0 count = 0 while (((i + count) < n) and (arr[i + count] == 1)): count += 1 # Update maximum number of consecutive # 1's found so far and its position if (count > max_ones): max_ones = count left_index = i # If maximum number of consecutive 1's # is equal to k, then return the minimum # number of swaps required to convert # binary array into a string of k consecutive 1's if (max_ones == k): return min(n - left_index - k, left_index) # If maximum number of consecutive 1's # is less than k, then return INVALID return -1

We can use a sliding window to keep track of the number of consecutive ones. If the number of ones is less than k, we increment the left pointer. If the number of ones is greater than k, we increment the right pointer. If the number of ones is equal to k, we update the minimum number of swaps.

The minimum number of adjacent swaps required to convert a binary string into a string with k consecutive 1's is given by: k - 1 if k is odd k/2 if k is even For example, if k = 3, the minimum number of adjacent swaps required is 2. If k = 4, the minimum number of adjacent swaps required is 2.

public int MinAdjSwaps(int[] A, int K) { // Base case if (A.Length == 0 || K == 0) return 0; int n = A.Length, i = 0, j = 0, count = 0, result = 0; // Find the first K 1's while (i < n) { if (A[i] == 1) { count++; } if (count == K) { break; } i++; } // Traverse the array from the first K 1's j = i; while (j < n) { // If we find a 1 if (A[j] == 1) { // Increment the result result++; // We are essentially swapping the position of // the current 1 with the previous 0 j = j + 1; } // If we find a 0 else { // We need to find the position of the next 1 // and swap with the current 0 int k = j + 1; while (k < n && A[k] != 1) { k++; } // Swap A[j] and A[k] int temp = A[j]; A[j] = A[k]; A[k] = temp; // Increment the result result++; // Move to the next position j = k + 1; } } return result; }