Minimum number of flips to make a Binary String increasing Last Updated : 07 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Given a binary string S, the task is to find the minimum number of characters the needed to be flipped to make the given binary string increasing. Example: Input: S = "00110"Output: 1Explanation: Flip S[4] = '0' to '1' of the string modifies the given string to "00111". Therefore, the minimum number of flips required is 1. Input: S = "010110"Output: 2 Approach: The given problem can be solved by using a Greedy Algorithm based on the observations that the resultant monotonically increasing string after any number of flips will be of the form ('0'*p + '1'*q), where p and q are the count of 0s and 1s respectively in the modified string. The idea is to traverse the given string S and for each index, i modify the substring S[0, i) to 0s and substring S[i, N) to 1s and find the minimum flips required accordingly. Follow the steps below to solve the problem: Find the count of 0s in the given binary string S and store it in a variable countZero.Initialize variable, say minFlips as (N - cntZero) that stores the minimum number of flips required.Initialize variable, say cntOne as 0 that stores the count of 1s in the string while traversing the string.Traverse the given string S and perform the following steps:If the character S[i] is 0, then decrement the value of countZero by 1.Otherwise, update the value of minFlips as the minimum of minFlips and (countZero + countOne) and increment the value of countOne by 1.After completing the above steps, print the value of minFlips as the result. Below is the implementation of the above approach. C++ // C++ program for the above approach; #include <bits/stdc++.h> using namespace std; // Function to find the minimum number of // flips required to make string increasing int minimumFlips(string s) { // Length of s int n = s.size(); // Total number of zero in s int cnt0 = count(s.begin(), s.end(), '0'); // Stores count of 1s till ith index int cnt1 = 0; // stores the minimum count of flip int res = n - cnt0; // Traverse the given string for (int i = 0; i < n; i++) { if (s[i] == '0') { cnt0 -= 1; } // Update the value of res // and count of 1s else if (s[i] == '1') { res = min(res, cnt1 + cnt0); cnt1++; } } // Return the minimum number // of flips return res; } // Driver code int main() { // Given string string S = "000110"; // function call cout << minimumFlips(S); return 0; } // This code is contributed by parthmanchanda81 Java // Java program for the above approach; import java.util.*; class GFG { // Function to find the minimum number of // flips required to make String increasing static int minimumFlips(String s) { // Length of s int n = s.length(); // Total number of zero in s int cnt0 = count(s, '0'); // Stores count of 1s till ith index int cnt1 = 0; // stores the minimum count of flip int res = n - cnt0; // Traverse the given String for (int i = 0; i < n; i++) { if (s.charAt(i) == '0') { cnt0 -= 1; } // Update the value of res // and count of 1s else if (s.charAt(i) == '1') { res = Math.min(res, cnt1 + cnt0); cnt1++; } } // Return the minimum number // of flips return res; } private static int count(String s, char c) { int ans = 0; for (char i : s.toCharArray()) if (c == i) ans++; return ans; } // Driver code public static void main(String[] args) { // Given String String S = "000110"; // function call System.out.print(minimumFlips(S)); } } // This code is contributed by Princi Singh Python3 # Python program for the above approach # Function to find the minimum number of # flips required to make string increasing def minimumFlips(s): # Length of s n = len(s) # Total number of zero in s cnt0 = s.count('0') # Stores count of 1s till ith index cnt1 = 0 # Stores the minimum count of flips res = n - cnt0 # Traverse the given string S for i in range(n): if s[i] == '0': cnt0 -= 1 elif s[i] == '1': # Update the value of res # and count of 1s res = min(res, cnt1 + cnt0) cnt1 += 1 # Return the minimum number # of flips return res # Driver Code S = '000110' # Function Call print(minimumFlips(S)) C# using System; public class GFG { // Function to find the minimum number of // flips required to make String increasing static int minimumFlips(String s) { // Length of s int n = s.Length; // Total number of zero in s int cnt0 = count(s, '0'); // Stores count of 1s till ith index int cnt1 = 0; // stores the minimum count of flip int res = n - cnt0; // Traverse the given String for (int i = 0; i < n; i++) { if (s[i] == '0') { cnt0 -= 1; } // Update the value of res // and count of 1s else if (s[i] == '1') { res = Math.Min(res, cnt1 + cnt0); cnt1++; } } // Return the minimum number // of flips return res; } private static int count(String s, char c) { int ans = 0; for (int j = 0; j < s.Length; j++) { char i = s[j]; if (c == i) ans++; } return ans; } // Driver code static public void Main() { // Given String String S = "000110"; // function call Console.Write(minimumFlips(S)); } } // This code is contributed by maddler. JavaScript <script> // JavaScript program for the above approach; // Function to find the minimum number of // flips required to make string increasing function minimumFlips(s) { // Length of s let n = s.length; // Total number of zero in s let i; let cnt0 = 0; for (i = 0; i < n; i++) { if (s[i] == '0') cnt0++; } // Stores count of 1s till ith index let cnt1 = 0 // Stores the minimum count of flips let res = n - cnt0 // Traverse the given string S for (i = 0; i < n; i++) if (s[i] == '0') { cnt0 -= 1 } else if (s[i] == '1') { // Update the value of res // and count of 1s res = Math.min(res, cnt1 + cnt0) cnt1 += 1 } // Return the minimum number // of flips return res } // Driver Code S = '000110' // Function Call document.write(minimumFlips(S)) // This code is contributed by Potta Lokesh </script> Output: 1 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum number of flips to make a Binary String increasing A adityamutharia Follow Improve Article Tags : Strings C/C++ Puzzles DSA binary-string Practice Tags : Strings Similar Reads Minimum number of flips with rotation to make binary string alternating Given a binary string S of 0s and 1s. The task is to make the given string a sequence of alternate characters by using the below operations: Remove some prefix from the start and append it to the end.Flip some or every bit in the given string. Print the minimum number of bits to be flipped to make t 11 min read Number of flips to make binary string alternate | Set 1 Given a binary string, that is it contains only 0s and 1s. We need to make this string a sequence of alternate characters by flipping some of the bits, our goal is to minimize the number of bits to be flipped. Examples : Input : str = â001â Output : 1 Minimum number of flips required = 1 We can flip 8 min read Minimum number of flipping adjacent bits required to make given Binary Strings equal Given two binary strings s1[] and s2[] of the same length N, the task is to find the minimum number of operations to make them equal. Print -1 if it is impossible to do so. One operation is defined as choosing two adjacent indices of one of the binary string and inverting the characters at those pos 7 min read Minimize Suffix flip to make Binary String non decreasing Given a binary string str, the task is to find the minimum operations required to convert the binary string in a non-decreasing manner such that in each operation an index i is chosen and all bits right to it are flipped. Examples: Input: str = "100010"Output: 3Explanation: On flipping at index 0 it 7 min read Minimum flips required to keep all 1s together in a Binary string Given binary string str, the task is to find the minimum number of flips required to keep all 1s together in the given binary string, i.e. there must not be any 0 between 1s in the string. Examples: Input: str = "0011111100" Output: 0 Explanation: We dont need to flip any bits because all the ones a 8 min read Minimize cost of flipping or swaps to make a Binary String balanced Given a binary string S of size N(where N is even), the task is to find the minimum cost of making the given binary string balanced by flipping one of the adjacent different characters at the cost of 1 unit or swap the characters at indices i and j such that (i < j) at the cost of (j - i) unit. I 6 min read Minimum Count of Bit flips required to make a Binary String Palindromic Given an integer N, the task is to find the minimum number of bits required to be flipped to convert the binary representation of N into a palindrome. Examples: Input: N = 12 Output: 2 Explanation: Binary String representing 12 = "1100". To make "1100" a palindrome, convert the string to "0110". The 7 min read Minimum number of distinct powers of 2 required to express a given binary number Given a binary string S, the task is to find the minimum number of Powers of 2 required to express a S.Examples: Input: S = "111" Output: 2 Explanation: Two possible representations of "111" (= 7) using powers of 2 are: 20 + 21 + 22 = 1 + 2 + 4 = 7 23 - 20 = 8 - 1 = 7 Therefore, the minimum powers o 6 min read Minimum number of operations required to obtain a given Binary String Given a binary strings S of length N, the task is to obtain S from a string, say T, of length N consisting only of zeroes, by minimum number of operations. Each operation involves choosing any index i from string S and flipping all the bits at indices [i, N - 1] of the string T. Examples: Input: S = 8 min read Minimum flips to remove any consecutive 3 0s or 1s in given Binary string Given a binary string S consisting of N characters, the task is to find the minimum number of flips required such that there don't exist three consecutive same characters. Examples: Input: S = "1100011"Output: 1Explanation:Flip the character at index 3 modifies the string S "1101011" that have no th 5 min read Like