Minimum flips to make all 1s in right and 0s in left
Given binary string str, the task is to find the minimum number of characters required to be flipped to make all 1s in right and 0s in left.
Examples:
Input: str = “100101”
Output: 2
Explanation:
Flipping str[0] and str[4] modifies str = “000111”. Therefore, the required output is 2.
Input: S = “00101001”
Output: 2
Explanation:
Flipping str[2] and str[4] modifies str = “00000001”. Therefore, the required output is 2.
Approach: The idea is to count the number of 0s on the right side of each index of the string and count the number of 1s on the left side of each index of the string. Follow the steps below to solve the problem:
- Initialize a variable, say cntzero, to store the total count of 0s in the given string.
- Traverse the string and count the total number of 0s in the given string.
- If cntzero in the given string is 0, or equal to the length of the string, the result will be 0.
- Traverse the string and for each index, find the sum of the count of 1s on the left side of the index and the count of 0s on the right side of the index.
- Find the minimum value from all sums obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumCntOfFlipsRequired(string str)
{
int n = str.length();
int zeros = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == '0' ) {
zeros++;
}
}
if (zeros == 0 || zeros == n) {
return 0;
}
int minFlips = INT_MAX;
int currOnes = 0;
int flips;
for ( int i = 0; i < n; i++) {
if (str[i] == '1' ) {
currOnes++;
}
flips = currOnes + (zeros - (i + 1 - currOnes));
minFlips = min(minFlips, flips);
}
return minFlips;
}
int main()
{
string str = "100101" ;
cout << minimumCntOfFlipsRequired(str);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int minimumCntOfFlipsRequired(
String str)
{
int n = str.length();
int zeros = 0 ;
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) == '0' ) {
zeros++;
}
}
if (zeros == 0 || zeros == n) {
return 0 ;
}
int minFlips = Integer.MAX_VALUE;
int currOnes = 0 ;
int flips;
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) == '1' ) {
currOnes++;
}
flips = currOnes + (zeros - (i + 1 - currOnes));
minFlips = Math.min(minFlips, flips);
}
return minFlips;
}
public static void main(String[] args)
{
String s1 = "100101" ;
System.out.println(minimumCntOfFlipsRequired(s1));
}
}
|
Python3
def minimumCntOfFlipsRequired( str ):
n = len ( str );
zeros = 0 ;
for i in range (n):
if ( str [i] = = '0' ):
zeros + = 1 ;
if (zeros = = 0 or zeros = = n):
return 0 ;
minFlips = 10000001 ;
currOnes = 0 ;
flips = 0 ;
for i in range (n):
if ( str [i] = = '1' ):
currOnes + = 1 ;
flips = currOnes + (zeros - (i + 1 - currOnes));
minFlips = min (minFlips, flips);
return minFlips;
if __name__ = = '__main__' :
str = "100101" ;
print (minimumCntOfFlipsRequired( str ));
|
C#
using System;
class GFG{
public static int minimumCntOfFlipsRequired( string str)
{
int n = str.Length;
int zeros = 0;
for ( int i = 0; i < n; i++)
{
if (str[i] == '0' )
{
zeros++;
}
}
if (zeros == 0 || zeros == n)
{
return 0;
}
int minFlips = Int32.MaxValue;
int currOnes = 0;
int flips;
for ( int i = 0; i < n; i++)
{
if (str[i] == '1' )
{
currOnes++;
}
flips = currOnes +
(zeros - (i + 1 - currOnes));
minFlips = Math.Min(minFlips, flips);
}
return minFlips;
}
public static void Main()
{
string s1 = "100101" ;
Console.WriteLine(minimumCntOfFlipsRequired(s1));
}
}
|
Javascript
<script>
function minimumCntOfFlipsRequired(str)
{
let n = str.length;
let zeros = 0;
for (let i = 0; i < n; i++) {
if (str[i] == '0' ) {
zeros++;
}
}
if (zeros == 0 || zeros == n) {
return 0;
}
let minFlips = Number.MAX_VALUE;
let currOnes = 0;
let flips;
for (let i = 0; i < n; i++) {
if (str[i] == '1' ) {
currOnes++;
}
flips = currOnes + (zeros - (i + 1 - currOnes));
minFlips = Math.min(minFlips, flips);
}
return minFlips;
}
let s1 = "100101" ;
document.write(minimumCntOfFlipsRequired(s1));
</script>
|
Time Complexity: O(N), where N is the length of the string
Auxiliary Space: O(1)
Last Updated :
22 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...