# Minimum number of flips to make a Binary String increasing

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: 1
Explanation: Flip S = ‘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 ` `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

 ``

Output:

`1`

Time Complexity: O(N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next