Wednesday, September 25, 2024
Google search engine
HomeData Modelling & AIMinimum swaps to group all 0s together in Binary Circular Array

Minimum swaps to group all 0s together in Binary Circular Array

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, 1, 0, 0, 0, 1, 1} using 1 swap.
  2. {1, 0, 0, 0, 1, 1, 1} using 1 swap.
  3. {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>


 
 

Output

1

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments