Given a binary circular array arr[] of size N, the task is to find the minimum swaps to group all 0s together in the array.
Examples:
Input: arr[] = {1, 0, 1, 0, 0, 1, 1}
Output: 1
Explanation: Here are a few of the ways to group all the 0’s together:
- {1, 1, 0, 0, 0, 1, 1} using 1 swap.
- {1, 0, 0, 0, 1, 1, 1} using 1 swap.
- {0, 0, 1, 1, 1, 1, 0} using 2 swaps (using the circular property of the array).
There is no way to group all 0’s together with 0 swaps. Thus, the minimum number of swaps required is 1.
Input: arr[] = {0, 0, 1, 1, 0}
Output: 0
Explanation: All the 0’s are already grouped together due to the circular property of the array.
Thus, the minimum number of swaps required is 0.
Approach: The task can be solved using the sliding window technique. Follow the below steps to solve the problem:
- Count the total number of 0s. Let m be that number
- Find the contiguous region of length m that has the most 0s in it
- The number of 1s in this region is the minimum required swaps. Each swap will move one 0 into the region and one 1 out of the region.
- Finally, use modulo operation for handling the case of the circular arrays.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minSwaps( int nums[], int N)
{
int count_1 = 0;
if (N == 1)
return 0;
for ( int i = 0; i < N; i++) {
if (nums[i] == 0)
count_1++;
}
int windowsize = count_1;
count_1 = 0;
for ( int i = 0; i < windowsize; i++) {
if (nums[i] == 0)
count_1++;
}
int mx = count_1;
for ( int i = windowsize; i < N + windowsize; i++) {
if (nums[i % N] == 0)
count_1++;
if (nums[(i - windowsize) % N] == 0)
count_1--;
mx = max(count_1, mx);
}
return windowsize - mx;
}
int main()
{
int nums[] = { 1, 0, 1, 0, 0, 1, 1 };
int N = sizeof (nums) / sizeof (nums[0]);
cout << minSwaps(nums, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int minSwaps( int nums[], int N)
{
int count_1 = 0 ;
if (N == 1 )
return 0 ;
for ( int i = 0 ; i < N; i++) {
if (nums[i] == 0 )
count_1++;
}
int windowsize = count_1;
count_1 = 0 ;
for ( int i = 0 ; i < windowsize; i++) {
if (nums[i] == 0 )
count_1++;
}
int mx = count_1;
for ( int i = windowsize; i < N + windowsize; i++) {
if (nums[i % N] == 0 )
count_1++;
if (nums[(i - windowsize) % N] == 0 )
count_1--;
mx = Math.max(count_1, mx);
}
return windowsize - mx;
}
public static void main (String[] args) {
int nums[] = { 1 , 0 , 1 , 0 , 0 , 1 , 1 };
int N = nums.length;
System.out.println( minSwaps(nums, N));
}
}
|
Python3
def minSwaps(nums, N):
count_1 = 0
if (N = = 1 ):
return 0
for i in range ( 0 , N):
if (nums[i] = = 0 ):
count_1 + = 1
windowsize = count_1
count_1 = 0
for i in range ( 0 , windowsize):
if (nums[i] = = 0 ):
count_1 + = 1
mx = count_1
for i in range (windowsize, N + windowsize):
if (nums[i % N] = = 0 ):
count_1 + = 1
if (nums[(i - windowsize) % N] = = 0 ):
count_1 - = 1
mx = max (count_1, mx)
return windowsize - mx
if __name__ = = "__main__" :
nums = [ 1 , 0 , 1 , 0 , 0 , 1 , 1 ]
N = len (nums)
print (minSwaps(nums, N))
|
C#
using System;
class GFG
{
static int minSwaps( int []nums, int N)
{
int count_1 = 0;
if (N == 1)
return 0;
for ( int i = 0; i < N; i++) {
if (nums[i] == 0)
count_1++;
}
int windowsize = count_1;
count_1 = 0;
for ( int i = 0; i < windowsize; i++) {
if (nums[i] == 0)
count_1++;
}
int mx = count_1;
for ( int i = windowsize; i < N + windowsize; i++) {
if (nums[i % N] == 0)
count_1++;
if (nums[(i - windowsize) % N] == 0)
count_1--;
mx = Math.Max(count_1, mx);
}
return windowsize - mx;
}
public static void Main()
{
int []nums = { 1, 0, 1, 0, 0, 1, 1 };
int N = nums.Length;
Console.Write(minSwaps(nums, N));
}
}
|
Javascript
<script>
function minSwaps(nums, N) {
let count_1 = 0;
if (N == 1)
return 0;
for (let i = 0; i < N; i++) {
if (nums[i] == 0)
count_1++;
}
let windowsize = count_1;
count_1 = 0;
for (let i = 0; i < windowsize; i++) {
if (nums[i] == 0)
count_1++;
}
let mx = count_1;
for (let i = windowsize; i < N + windowsize; i++) {
if (nums[i % N] == 0)
count_1++;
if (nums[(i - windowsize) % N] == 0)
count_1--;
mx = Math.max(count_1, mx);
}
return windowsize - mx;
}
let nums = [1, 0, 1, 0, 0, 1, 1];
let N = nums.length;
document.write(minSwaps(nums, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)