Given a 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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the minimum // changes required int minChanges(string str, int n) { // To store the count of minimum changes, // number of ones and the number of zeroes int count = 0, zeros = 0, ones = 0; // First character has to be '1' if (str[0] != '1') { count++; ones++; } for (int i = 1; i < n; i++) { if (str[i] == '0') zeros++; else ones++; // If condition fails // changes need to be made if (zeros > ones) { zeros--; ones++; count++; } } // Return the required count return count; } // Driver code int main() { string str = "0000"; int n = str.length(); cout << minChanges(str, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the minimum // changes required static int minChanges(char[] str, int n) { // To store the count of minimum changes, // number of ones and the number of zeroes int count = 0, zeros = 0, ones = 0; // First character has to be '1' if (str[0] != '1') { count++; ones++; } for (int i = 1; i < n; i++) { if (str[i] == '0') zeros++; else ones++; // If condition fails // changes need to be made if (zeros > ones) { zeros--; ones++; count++; } } // Return the required count return count; } // Driver code public static void main(String[] args) { char []str = "0000".toCharArray(); int n = str.length; System.out.print(minChanges(str, n)); } } // This code has been contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach # Function to return the minimum # changes required def minChanges(str, n): # To store the count of minimum changes, # number of ones and the number of zeroes count, zeros, ones = 0, 0, 0 # First character has to be '1' 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 condition fails # changes need to be made if (zeros > ones): zeros -= 1 ones += 1 count += 1 # Return the required count return count # Driver code if __name__ == '__main__': str = "0000" n = len(str) print(minChanges(str, n)) # This code contributed by PrinciRaj1992 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the minimum // changes required static int minChanges(char[] str, int n) { // To store the count of minimum changes, // number of ones and the number of zeroes int count = 0, zeros = 0, ones = 0; // First character has to be '1' if (str[0] != '1') { count++; ones++; } for (int i = 1; i < n; i++) { if (str[i] == '0') zeros++; else ones++; // If condition fails // changes need to be made if (zeros > ones) { zeros--; ones++; count++; } } // Return the required count return count; } // Driver code public static void Main(String[] args) { char []str = "0000".ToCharArray(); int n = str.Length; Console.Write(minChanges(str, n)); } } // This code contributed by Rajput-Ji |
PHP
<?php // PHP implementation of the approach // Function to return the minimum // changes required function minChanges($str, $n) { // To store the count of minimum changes, // number of ones and the number of zeroes $count = $zeros = $ones = 0; // First character has to be '1' if ($str[0] != '1') { $count++; $ones++; } for ($i = 1; $i < $n; $i++) { if ($str[$i] == '0') $zeros++; else $ones++; // If condition fails // changes need to be made if ($zeros > $ones) { $zeros--; $ones++; $count++; } } // Return the required count return $count; } // Driver code $str = "0000"; $n = strlen($str); echo minChanges($str, $n); // This code is contributed by mits ?> |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if the string satisfies the given condition
- Find the lexicographically smallest string which satisfies the given condition
- Minimum changes required to make first string substring of second string
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum operations required to make the string satisfy the given condition
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Minimum changes to a string to make all substrings distinct
- Minimum number of given operations required to convert a string to another string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum size binary string required such that probability of deleting two 1's at random is 1/X
- Check if given Binary string follows then given condition or not
- Minimum given operations required to convert a given binary string to all 1's
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum string such that every adjacent character of given string is still adjacent
- Minimum bit changes in Binary Circular array to reach a index
- Perform range sum queries on string as per given condition
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum number of operations required to obtain a given Binary String
- Minimum operations required to convert all characters of a String to a given Character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

