Minimum swaps to group all 0s together in Binary Circular Array Last Updated : 28 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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: 1Explanation: 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: 0Explanation: 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 numberFind the contiguous region of length m that has the most 0s in itThe 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> Output1 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum swaps to group all 0s together in Binary Circular Array sauarbhyadav Follow Improve Article Tags : Mathematical Competitive Programming Algo Geek DSA Arrays binary-representation sliding-window circular-array Swap-Program +5 More Practice Tags : ArraysMathematicalsliding-window 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 Minimum Group Flips to Make Binary Array Elements Same The problem statement asks us to find the minimum number of group flips required to convert a given binary array into an array that either contains all 1s or all 0s. A group flip is defined as flipping a continuous sequence of elements in the array. For example, consider the binary array {1, 1, 0, 0 11 min read Minimum shifts of substrings of 1s required to group all 1s together in a given Binary string Given a binary string S of length N, the task is to print the minimum number of indices, substrings consisting only of 1s are required to be shifted such that all 1s present in the string are grouped together. Examples: Input: S = "00110111011"Output: 2Explanation: Operation 1: Shift substring {S[2] 6 min read Minimum Subarray flips required to convert all elements of a Binary Array to K The problem statement is asking for the minimum number of operations required to convert all the elements of a given binary array arr[] to a specified value K, where K can be either 0 or 1. The operations can be performed on any index X of the array, and the operation is to flip all the elements of 8 min read Minimum length subarray of 1s in a Binary Array Given binary array. The task is to find the length of subarray with minimum number of 1s.Note: It is guaranteed that there is atleast one 1 present in the array.Examples : Input : arr[] = {1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1} Output : 3 Minimum length subarray of 1s is {1, 1}.Input : arr[] = {0, 0, 1 8 min read Minimum time required to cover a Binary Array Given an integer N which represents the size of a boolean array that is initially filled with 0, and also given an array arr[] of size K consisting of (1-based) indices at which '1' are present in the boolean array. Now, at one unit of time the adjacent cells of the arr[i] in the boolean array becom 8 min read Like