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

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 = 3

Input: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++

`// C++ program for the above approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of minimum flips required ` `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[i] == curr) ` ` ` `{ ` ` ` `count++; ` ` ` ` ` `// Switch curr to '0' if '1' ` ` ` `// or vice-versa ` ` ` `curr = (` `char` `)(48 + (curr + 1) % 2); ` ` ` `} ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string S = ` `"011000"` `; ` ` ` ` ` `cout << (minFlips(S)); ` `} ` ` ` `// This code is contributed by rock_cool ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `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 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 code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `String S = ` `"011000"` `; ` ` ` `Console.WriteLine(minFlips(S)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum flips required to keep all 1s together in a Binary string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to maximize a number with k set bits
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Find Bit whose minimum sequence flips makes all bits same
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Count minimum bits to flip such that XOR of A and B equal to C
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum number of operations required to sum to binary string S
- Minimum given operations required to convert a given binary string to all 1's
- Minimum swaps required to convert one binary string to another
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum swaps required to make a binary string alternating
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Number of flips to make binary string alternate | Set 1
- Minimum number of given operations required to convert a string to another string
- Minimum changes required to make first string substring of second string
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.

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.