Minimum swaps to group all 0s together in Binary Circular Array
Last Updated :
28 Jan, 2022
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count min swap
// to get all zeros together
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++;
}
// Window size for counting
// maximum no. of 1s
int windowsize = count_1;
count_1 = 0;
for (int i = 0; i < windowsize; i++) {
if (nums[i] == 0)
count_1++;
}
// For storing maximum count of 1s in
// a window
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;
}
// Driver code
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
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to count min swap
// to get all zeros together
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++;
}
// Window size for counting
// maximum no. of 1s
int windowsize = count_1;
count_1 = 0;
for (int i = 0; i < windowsize; i++) {
if (nums[i] == 0)
count_1++;
}
// For storing maximum count of 1s in
// a window
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;
}
// Driver code
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));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# python3 program for the above approach
# Function to count min swap
# to get all zeros together
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
# Window size for counting
# maximum no. of 1s
windowsize = count_1
count_1 = 0
for i in range(0, windowsize):
if (nums[i] == 0):
count_1 += 1
# For storing maximum count of 1s in
# a window
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
# Driver code
if __name__ == "__main__":
nums = [1, 0, 1, 0, 0, 1, 1]
N = len(nums)
print(minSwaps(nums, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count min swap
// to get all zeros together
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++;
}
// Window size for counting
// maximum no. of 1s
int windowsize = count_1;
count_1 = 0;
for (int i = 0; i < windowsize; i++) {
if (nums[i] == 0)
count_1++;
}
// For storing maximum count of 1s in
// a window
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;
}
// Driver code
public static void Main()
{
int []nums = { 1, 0, 1, 0, 0, 1, 1 };
int N = nums.Length;
Console.Write(minSwaps(nums, N));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the above approach
// Function to count min swap
// to get all zeros together
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++;
}
// Window size for counting
// maximum no. of 1s
let windowsize = count_1;
count_1 = 0;
for (let i = 0; i < windowsize; i++) {
if (nums[i] == 0)
count_1++;
}
// For storing maximum count of 1s in
// a window
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;
}
// Driver code
let nums = [1, 0, 1, 0, 0, 1, 1];
let N = nums.length;
document.write(minSwaps(nums, N));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximum consecutive oneâs (or zeros) in a binary circular array Given a binary circular array of size N, the task is to find the count maximum number of consecutive 1âs present in the circular array.Examples: Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1} Output: 6 The last 4 and first 2 positions have 6 consecutive ones. Input: a[] = {0, 1, 0, 1, 0, 1, 0,
8 min read
Minimum Swaps required to group all 1's together Given an array of 0's and 1's, we need to write a program to find the minimum number of swaps required to group all 1's present in the array together.Examples: Input: arr[] = [1, 0, 1, 0, 1]Output: 1Explanation: Only 1 swap is required to group all 1's together. Swapping index 1 with 4 will give arr
9 min read
Minimum bit changes in Binary Circular array to reach a index Given a Binary Circular Array of size N elements and two positive integers x and y indicating the indices in the circular array. The task is check which path, clockwise or anti-clockwise, from index x to index y, we face the minimum number bit flips. Output "Clockwise" or "Anti-clockwise" and the va
9 min read
Minimum flips required to keep all 1s together in a Binary string Given binary string str, the task is to find the minimum number of flips required to keep all 1s together in the given binary string, i.e. there must not be any 0 between 1s in the string. Examples: Input: str = "0011111100" Output: 0 Explanation: We dont need to flip any bits because all the ones a
8 min read
Minimum toggles to partition a binary array so that it has first 0s then 1s Given an array of n integers containing only 0 and 1. Find the minimum toggles (switch from 0 to 1 or vice-versa) required such the array become partitioned, i.e., it has first 0s than 1s. There should be at least one 0 in the beginning, and there can be zero or more 1s in the end. Input: arr[] = {1
7 min read