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.
- Take binary array as input
- Convert array to string and then the equivalent decimal number(num)
- Take initial mask value and iterate till num <= 0
- Find required flips using (num XOR mask)
- Find minimum flips and decrease num and update mask
- Return the minimum count
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countones( long n);
int findMiniFlip( int nums[], int n)
{
string s = "" ;
for ( int i = 0; i < n; i++)
s += nums[i];
char *end;
char tmp[s.length()];
strcpy (tmp, s.c_str());
long num = strtol (tmp, &end, 2);
int minXor = n;
long mask = (1 << (n - 1));
while (n - 1 > 0)
{
long temp = (num ^ mask);
minXor = min(minXor, countones(temp));
n--;
mask = (mask | (1 << (n - 1)));
}
return minXor;
}
int countones( long n)
{
int c = 0;
while (n > 0)
{
n = n & (n - 1);
c++;
}
return c;
}
int main()
{
int nums[] = {1, 0, 1,
1, 0, 0, 0};
int size = sizeof (nums) /
sizeof (nums[0]);
int n = findMiniFlip(nums, size);
cout << n;
}
|
Java
import java.io.*;
class GFG {
public static int findMiniFlip( int [] nums)
{
int n = nums.length;
String s = "" ;
for ( int i = 0 ; i < n; i++)
s += nums[i];
long num = Integer.parseInt(s, 2 );
int minXor = n;
long mask = ( 1 << (n- 1 ));
while (n- 1 > 0 ) {
long temp = (num ^ mask);
minXor = Math.min(minXor, countones(temp));
n--;
mask = (mask | ( 1 << n - 1 ));
}
return minXor;
}
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);
}
}
|
Python3
def findMiniFlip(nums):
n = len (nums)
s = ''
for i in range (n):
s + = str (nums[i])
num = int (s, 2 )
minXor = n;
mask = ( 1 << (n - 1 ))
while (n - 1 > 0 ):
temp = (num ^ mask)
minXor = min (minXor, countones(temp))
n - = 1
mask = (mask | ( 1 << n - 1 ))
return minXor
def countones(n):
c = 0
while (n > 0 ):
n = n & (n - 1 )
c + = 1
return c
if __name__ = = "__main__" :
nums = [ 1 , 0 , 1 , 1 , 0 , 0 , 0 ]
n = findMiniFlip(nums)
print (n)
|
C#
using System;
class GFG{
public static int findMiniFlip( int [] nums)
{
int n = nums.Length;
String s = "" ;
for ( int i = 0; i < n; i++)
s += nums[i];
long num = Convert.ToInt32(s, 2);
int minXor = n;
long mask = (1 << (n - 1));
while (n - 1 > 0)
{
long temp = (num ^ mask);
minXor = Math.Min(minXor,
countones(temp));
n--;
mask = (mask | (1 << n - 1));
}
return minXor;
}
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);
Console.WriteLine(n);
}
}
|
Javascript
<script>
function findMiniFlip(nums)
{
let n = nums.length;
let s = "" ;
for (let i = 0; i < n; i++)
s += nums[i];
let num = parseInt(s, 2);
let minXor = n;
let mask = (1 << (n - 1));
while (n - 1 > 0)
{
let temp = (num ^ mask);
minXor = Math.min(minXor, countones(temp));
n--;
mask = (mask | (1 << n -1));
}
return minXor;
}
function countones(n)
{
let c = 0;
while (n > 0)
{
n = n & (n - 1);
c++;
}
return c;
}
let nums = [ 1, 0, 1, 1, 0, 0, 0 ];
let n = findMiniFlip(nums);
document.write(n);
</script>
|
Output:
1
Time Complexity: O(n)
Space Complexity: O(n)