Minimum flips to make all 1s in left and 0s in right | Set 2
Given a binary string, we can flip any bit in the given string i.e., convert 1 to 0 or vice-versa. Calculate the minimum flips required to make all 1s on the left and all 0s on the right.
Examples :
Input: 1011000
Output: 1
Explanation: 1 flip is required to make it 1111000.
Input: 00001
Output: 2
Explanation: 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minimalFilps(string bits)
{
int n = bits.length();
int flipsFromLeft[n];
int flipsFromRight[n];
int flips = 0;
for ( int i = 0; i < n; i++) {
if (bits[i] == '0' )
flips++;
flipsFromLeft[i] = flips;
}
flips = 0;
for ( int i = n - 1; i >= 0; i--) {
if (bits[i] == '1' )
flips++;
flipsFromRight[i] = flips;
}
int minFlips = INT_MAX;
for ( int i = 1; i < n; i++) {
if (flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips)
minFlips = flipsFromLeft[i - 1] + flipsFromRight[i];
}
return minFlips;
}
int main()
{
string bits = "00001" ;
cout << minimalFilps(bits) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int minimalFilps(String bits)
{
int n = bits.length();
int flipsFromLeft[] = new int [n];
int flipsFromRight[] = new int [n] ;
int flips = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (bits.charAt(i) == '0' )
flips++;
flipsFromLeft[i] = flips;
}
flips = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--)
{
if (bits.charAt(i) == '1' )
flips++;
flipsFromRight[i] = flips;
}
int minFlips = Integer.MAX_VALUE;
for ( int i = 1 ; i < n; i++)
{
if (flipsFromLeft[i - 1 ] + flipsFromRight[i]
< minFlips)
minFlips = flipsFromLeft[i - 1 ]
+ flipsFromRight[i];
}
return minFlips;
}
public static void main (String[] args)
{
String bits = "00001" ;
System.out.println(minimalFilps(bits));
}
}
|
Python3
import sys
def minimalFilps(bits):
n = len (bits)
flipsFromLeft = [ 0 for i in range (n)]
flipsFromRight = [ 0 for i in range (n)]
flips = 0
for i in range ( 0 , n, 1 ):
if (bits[i] = = '0' ):
flips = flips + 1
flipsFromLeft[i] = flips
flips = 0
i = n - 1
while (i > = 0 ):
if (bits[i] = = '1' ):
flips = flips + 1
i = i - 1
flipsFromRight[i] = flips
minFlips = sys.maxsize
for i in range ( 1 , n, 1 ):
if (flipsFromLeft[i - 1 ] +
flipsFromRight[i] < minFlips):
minFlips = (flipsFromLeft[i - 1 ] +
flipsFromRight[i])
return minFlips
if __name__ = = '__main__' :
bits = "00001"
print (minimalFilps(bits))
|
C#
using System;
class GFG
{
static int minimalFilps(String bits)
{
int n = bits.Length;
int []flipsFromLeft = new int [n];
int []flipsFromRight = new int [n] ;
int flips = 0;
for ( int i = 0; i < n; i++)
{
if (bits[i] == '0' )
flips++;
flipsFromLeft[i] = flips;
}
flips = 0;
for ( int i = n - 1; i >= 0; i--)
{
if (bits[i] == '1' )
flips++;
flipsFromRight[i] = flips;
}
int minFlips = int .MaxValue;
for ( int i = 1; i < n; i++)
{
if (flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips)
minFlips = flipsFromLeft[i - 1] + flipsFromRight[i];
}
return minFlips;
}
public static void Main ()
{
string bits = "00001" ;
Console.WriteLine(minimalFilps(bits));
}
}
|
PHP
<?php
function minimalFilps( $bits )
{
$n = strlen ( $bits );
$flipsFromLeft [ $n ] = 0;
$flipsFromRight [ $n ] = 0;
$flips = 0;
for ( $i = 0; $i < $n ; $i ++) {
if ( $bits [ $i ] == '0' )
$flips ++;
$flipsFromLeft [ $i ] = $flips ;
}
$flips = 0;
for ( $i = $n - 1; $i >= 0; $i --)
{
if ( $bits [ $i ] == '1' )
$flips ++;
$flipsFromRight [ $i ] = $flips ;
}
$INT_MAX =2147483647;
$minFlips = $INT_MAX ;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $flipsFromLeft [ $i - 1] +
$flipsFromRight [ $i ] < $minFlips )
$minFlips = $flipsFromLeft [ $i - 1] +
$flipsFromRight [ $i ];
}
return $minFlips ;
}
$bits = "00001" ;
echo minimalFilps( $bits ) ;
?>
|
Javascript
<script>
function minimalFilps(bits)
{
let n = bits.length;
let flipsFromLeft = new Array(n);
flipsFromLeft.fill(0);
let flipsFromRight = new Array(n);
flipsFromRight.fill(0);
let flips = 0;
for (let i = 0; i < n; i++)
{
if (bits[i] == '0' )
flips++;
flipsFromLeft[i] = flips;
}
flips = 0;
for (let i = n - 1; i >= 0; i--)
{
if (bits[i] == '1' )
flips++;
flipsFromRight[i] = flips;
}
let minFlips = Number.MAX_VALUE;
for (let i = 1; i < n; i++)
{
if (flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips)
minFlips = flipsFromLeft[i - 1] + flipsFromRight[i];
}
return minFlips;
}
let bits = "00001" ;
document.write(minimalFilps(bits));
</script>
|
Output
2
Time complexity: O(N) where N is the length of the given binary string.
Auxiliary space: O(N), for creating arrays flipsFromLeft and flipsFromRight of size N.
Last Updated :
05 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...