Minimum changes required such that the string satisfies the given condition
Given binary string str. In a single operation, we can change any ‘1’ to ‘0’ or any ‘0’ to ‘1’. The task is to make minimum number of changes in the string such that if we take any prefix of the string, the number of 1’s should be greater than or equal number of 0’s.
Examples:
Input: str = “10001”
Output: 1
We can change str[2] from ‘0’ to ‘1’.
Input: str = “0000”
Output: 2
Approach: The problem can be solved greedily. The first character of the string has to be 1. Then for the rest of the string, we traverse through the string character by character and check if the required condition is fulfilled or not, if not then we increase the count of changes required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minChanges(string str, int n)
{
int count = 0, zeros = 0, ones = 0;
if (str[0] != '1' ) {
count++;
ones++;
}
for ( int i = 1; i < n; i++) {
if (str[i] == '0' )
zeros++;
else
ones++;
if (zeros > ones) {
zeros--;
ones++;
count++;
}
}
return count;
}
int main()
{
string str = "0000" ;
int n = str.length();
cout << minChanges(str, n);
return 0;
}
|
Java
class GFG
{
static int minChanges( char [] str, int n)
{
int count = 0 , zeros = 0 , ones = 0 ;
if (str[ 0 ] != '1' )
{
count++;
ones++;
}
for ( int i = 1 ; i < n; i++)
{
if (str[i] == '0' )
zeros++;
else
ones++;
if (zeros > ones)
{
zeros--;
ones++;
count++;
}
}
return count;
}
public static void main(String[] args)
{
char []str = "0000" .toCharArray();
int n = str.length;
System.out.print(minChanges(str, n));
}
}
|
Python3
def minChanges( str , n):
count, zeros, ones = 0 , 0 , 0
if ( ord ( str [ 0 ])! = ord ( '1' )):
count + = 1
ones + = 1
for i in range ( 1 , n):
if ( ord ( str [i]) = = ord ( '0' )):
zeros + = 1
else :
ones + = 1
if (zeros > ones):
zeros - = 1
ones + = 1
count + = 1
return count
if __name__ = = '__main__' :
str = "0000"
n = len ( str )
print (minChanges( str , n))
|
C#
using System;
class GFG
{
static int minChanges( char [] str, int n)
{
int count = 0, zeros = 0, ones = 0;
if (str[0] != '1' )
{
count++;
ones++;
}
for ( int i = 1; i < n; i++)
{
if (str[i] == '0' )
zeros++;
else
ones++;
if (zeros > ones)
{
zeros--;
ones++;
count++;
}
}
return count;
}
public static void Main(String[] args)
{
char []str = "0000" .ToCharArray();
int n = str.Length;
Console.Write(minChanges(str, n));
}
}
|
PHP
<?php
function minChanges( $str , $n )
{
$count = $zeros = $ones = 0;
if ( $str [0] != '1' )
{
$count ++;
$ones ++;
}
for ( $i = 1; $i < $n ; $i ++)
{
if ( $str [ $i ] == '0' )
$zeros ++;
else
$ones ++;
if ( $zeros > $ones )
{
$zeros --;
$ones ++;
$count ++;
}
}
return $count ;
}
$str = "0000" ;
$n = strlen ( $str );
echo minChanges( $str , $n );
?>
|
Javascript
<script>
function minChanges(str, n)
{
let count = 0, zeros = 0, ones = 0;
if (str[0] != '1' )
{
count++;
ones++;
}
for (let i = 1; i < n; i++)
{
if (str[i] == '0' )
zeros++;
else
ones++;
if (zeros > ones)
{
zeros--;
ones++;
count++;
}
}
return count;
}
let str = "0000" .split( '' );
let n = str.length;
document.write(minChanges(str, n));
</script>
|
Time Complexity: O(n), where n is the size of the given string
Auxiliary Space: O(1)
Last Updated :
08 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...