Minimum flips required to form given binary string where every flip changes all bits to its right as well

13 Feb, 2022
Given a string S, the task is to find minimum flips required to convert an initial binary string consisting of only zeroes to S where every flip of a character flips all succeeding characters as well. Examples:  
Input: S = “01011” Output: 3 Explanation: Initial String – “00000” Flip the 2nd bit – “01111” Flip the 3rd bit – “01000” Flip the 4th bit – “01011” Total Flips = 3Input: S = “01001” Output: 3 Explanation: Initial String – “00000” Flip the 2nd bit – “01111” Flip the 3rd bit – “01000” Flip the 5th bit – “01001” Total Flips = 3  
 
Approach: In order to solve the problem, follow the steps below:  
Store ‘1’ in curr initially.
Traverse S and find the first occurrence of curr. Increase count when curr is encountered. Store ‘0’ if curr is ‘1’ or vice versa.
Repeat the above step for entire traversal of S.
Final value of count gives the required answer.
Below is the implementation of the above approach.
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include<bits/stdc++.h>using namespace std; // Function to return the count// of minimum flips requiredint minFlips(string target){    char curr = '1';    int count = 0;    for(int i = 0; i < target.length(); i++)    {                // If curr occurs in the final string       if (target[i] == curr)       {           count++;                       // Switch curr to '0' if '1'           // or vice-versa           curr = (char)(48 + (curr + 1) % 2);       }    }    return count;} // Driver Codeint main(){    string S = "011000";         cout << (minFlips(S));} // This code is contributed by rock_cool
// Java program for the above approachimport java.util.Arrays; public class GFG {    // Function to return the count of    // minimum flips required    public static int minFlips(String target)    {         char curr = '1';        int count = 0;        for (int i = 0; i < target.length(); i++) {             // If curr occurs in the final string            if (target.charAt(i) == curr) {                 count++;                 // Switch curr to '0' if '1'                // or vice-versa                curr = (char)(48 + (curr + 1) % 2);            }        }         return count;    }     // Driver Code    public static void main(String args[])    {         String S = "011000";        System.out.println(minFlips(S));    }}
# Python3 program for the above approach # Function to return the count# of minimum flips requireddef minFlips(target):     curr = '1'    count = 0         for i in range(len(target)):                 # If curr occurs in the final string        if (target[i] == curr):            count += 1                         # Switch curr to '0' if '1'            # or vice-versa            curr = chr(48 + (ord(curr) + 1) % 2)         return count # Driver Codeif __name__ == "__main__":         S = "011000"         print(minFlips(S)) # This code is contributed by chitranayal
// C# program for the above approachusing System; class GFG{     // Function to return the count of// minimum flips requiredpublic static int minFlips(String target){    char curr = '1';    int count = 0;    for(int i = 0; i < target.Length; i++)    {                // If curr occurs in the readonly string       if (target[i] == curr)       {           count++;                       // Switch curr to '0' if '1'           // or vice-versa           curr = (char)(48 + (curr + 1) % 2);       }    }    return count;} // Driver codepublic static void Main(String []args){    String S = "011000";    Console.WriteLine(minFlips(S));}} // This code is contributed by 29AjayKumar
<script>     // Javascript program for the above approach         // Function to return the count    // of minimum flips required    function minFlips(target)    {        let curr = '1';        let count = 0;        for(let i = 0; i < target.length; i++)        {            // If curr occurs in the final string           if (target[i] == curr)           {               count++;                // Switch curr to '0' if '1'               // or vice-versa               curr = String.fromCharCode(48 + (curr.charCodeAt() + 1) % 2);           }        }        return count;    }         let S = "011000";           document.write(minFlips(S));     </script>
2
Time Complexity: O(N) Auxiliary Space: O(1)
29AjayKumar
rock_cool
ukasp
mukesh07
rohitsingh07052
binary-string
Bit Magic
Strings
Strings
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Binary representation of a given number
Bits manipulation (Important tactics)
Add two numbers without using arithmetic operators
Rotate bits of a number
Reverse a string in Java
Write a program to reverse an array or string
C++ Data Types
KMP Algorithm for Pattern Searching
Write a program to print all permutations of a given string