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.
Input: 1011000 Output: 1 1 flip is required to make it 1111000. Input : 00001 Output : 2 2 flips required to make it 10000.
We have discussed a bitmask based solution in below post. Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
It can be done with O(N) time complexity (where N – number of bits) and O(N) extra memory
- Calculate number of flips of ‘0’ needed to be done while moving from left to right to have all ‘1’ in bits.
- Calculate number of flips of ‘1’ needed to be done while moving from right to left to have all ‘0’ in bits.
- Traversing through all positions between bits and find minimal sum of ‘0’-flips+’1′-flips from both arrays.
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Number of flips to make binary string alternate | Set 1
- Count minimum right flips to set all values in an array
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Find Bit whose minimum sequence flips makes all bits same
- Min flips of continuous characters to make all characters same in a string
- Find the minimum number of rectangles left after inserting one into another
- Minimum element left from the array after performing given operations
- Minimum operations to make XOR of array zero
- Minimum operations to make the MEX of the given set equal to x
- Minimum removals to make array sum even
- Minimum removals from array to make max - min <= K
- Minimum removals to make array sum odd
- Minimum changes required to make two arrays identical
- Minimum removals from array to make GCD greater
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.