Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)

Given a binary array, we can flip all the 1 are in the left part and all the 0 to the right part.Calculate the minimum flips required to make all 1s in left and all 0s in right.

Examples:

Input: 1011000  
Output: 1
1 flip is required to make it 1111000.

Input : 00001 
Output : 2
2 flips required to make it 10000.

For solving this problem we use bitmasking. First, we convert this array to string, then we find the equivalent decimal number of that binary string. We try all masks with all possibilities of 1s in left and 0s in right. We iterate a loop till decimal number becomes zero. Each time we will do bitwise XOR of the number with mask and number of ones in XOR value will be the number of flips required. We decrease n by 1 and update the mask.

1-Take binary array as input
2-Convert array to string and then equivalent decimal number(num)
3-Take initial mask value and iterate till num <= 0
4-Find required flips using (num XOR mask)
5-Find minimum flips and decrease num and update mask
6-Return the minimum count

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find minimum flips to make
// all 1s in left
import java.io.*;
  
class GFG {
  
    // function to count minimum number of flips
    public static int findMiniFlip(int[] nums)
    {
        int n = nums.length;
        String s = "";
        for (int i = 0; i < n; i++)
            s += nums[i];
  
        // This is converting string s into integer 
        // of base 2 (if s = '100' then num = 4)
        long num = Integer.parseInt(s, 2);
  
        // initialize minXor with n that can be maximum 
        // number of flips
        int minXor = n;
  
        // right shift 1 by (n-1) bits
        long mask = (1 << (n-1));
        while (n-1 > 0) {
  
            // calculate bitwise XOR of num and mask
            long temp = (num ^ mask);
  
            // Math.min(a, b) returns minimum of a and b
            // return minimum number of flips till that 
            // digit
            minXor = Math.min(minXor, countones(temp));
            n--;
  
            mask = (mask | (1 << n -1));
        }
        return minXor;
    }
  
    // function to count number of 1s
    public static int countones(long n)
    {
        int c = 0;
        while (n > 0) {
            n = n & (n-1);
            c++;
        }
        return c;
    }
  
    public static void main(String[] args)
    {
        int[] nums = { 1, 0, 1, 1, 0, 0, 0 };
        int n = findMiniFlip(nums);
        System.out.println(n);
    }
}

chevron_right


Output:

1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : prateek dhawalia



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.