![]() |
VOOZH | about |
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 a 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 the decimal number becomes zero. Each time we will do bitwise XOR of the number with mask and the number of ones in XOR value will be the number of flips required. We decrease n by 1 and update the mask.
Implementation:
Output:
1
Time Complexity: O(n)
Space Complexity: O(n)