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.
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Minimum flips required to keep all 1s together in a Binary string
- Count minimum right flips to set all values in an array
- Minimum flips required to maximize a number with k set bits
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Partition of a set into K subsets with equal sum using BitMask and DP
- Find the minimum number of rectangles left after inserting one into another
- Minimum element left from the array after performing given operations
- Minimum removals from array to make max - min <= K
- Minimum operations to make the MEX of the given set equal to x
- Minimum operations to make XOR of array zero
- Minimum removals to make array sum odd
- Minimum removals to make array sum even
- Minimum operations to make GCD of array a multiple of k
- Minimum number of changes required to make the given array an AP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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