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 = “101”Output:3Explanation:

“000” -> “111” -> “100” -> “101”.

Therefore, the minimum number of steps required is 3.Input:S = “10111”Output:3Explanation:

“00000” -> “11111” -> “10000” -> “10111”.

Therefore, the minimum number of steps required is 3.

**Approach:**

The idea is to find the first occurrence of **1** in the given string **S** and perform the given operation at that index. After this step, for every mismatch in the character of **S** and **T** at a particular index, repeat the opertion.

Follow the steps below:

- Iterate over
**S**and mark the first occurrence of**1**. - Initialize two variables, say
and**last**, where**ans****last**stores the character (**0 or 1**) for which the last operation was performed and**ans**keeps count of the minimum number of steps required. - Iterate from the first occurrence of
**1**till the end of the string. - If the current character is
**0**and**last = 1**, then increment the count of, and set**ans****last = 0**. - Otherwise, if
**last = 0**and the current character is**1**, then set**last = 1**. - Return the final value of
**ans**.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the minimum` `// number of operations required` `// to obtain the string s` `int` `minOperations(string s)` `{` ` ` `int` `n = s.size();` ` ` `int` `pos = -1;` ` ` `// Iterate the string s` ` ` `for` `(` `int` `i = 0; i < s.size(); i++) {` ` ` `// If first occurrence of 1` ` ` `// is found` ` ` `if` `(s[i] == ` `'1'` `) {` ` ` `// Mark the index` ` ` `pos = i;` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// Base case: If no 1 occurred` ` ` `if` `(pos == -1) {` ` ` `// No operations required` ` ` `return` `0;` ` ` `}` ` ` `// Stores the character` ` ` `// for which last operation` ` ` `// was performed` ` ` `int` `last = 1;` ` ` `// Stores minimum number` ` ` `// of operations` ` ` `int` `ans = 1;` ` ` `// Iterate from pos to n` ` ` `for` `(` `int` `i = pos + 1; i < n; i++) {` ` ` `// Check if s[i] is 0` ` ` `if` `(s[i] == ` `'0'` `) {` ` ` `// Check if last operation was` ` ` `// performed because of 1` ` ` `if` `(last == 1) {` ` ` `ans++;` ` ` `// Set last to 0` ` ` `last = 0;` ` ` `}` ` ` `}` ` ` `else` `{` ` ` `if` `(last == 0) {` ` ` `// Check if last operation was` ` ` `// performed because of 0` ` ` `ans++;` ` ` `// Set last to 1` ` ` `last = 1;` ` ` `}` ` ` `}` ` ` `}` ` ` `// Return the answer` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `string s = ` `"10111"` `;` ` ` `cout << minOperations(s);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement the` `// above approach` `import` `java.util.*;` `class` `GFG{` `// Function to find the minimum` `// number of operations required` `// to obtain the string s` `static` `int` `minOperations(String s)` `{` ` ` `int` `n = s.length();` ` ` `int` `pos = -` `1` `;` ` ` `// Iterate the string s` ` ` `for` `(` `int` `i = ` `0` `; i < s.length(); i++) ` ` ` `{` ` ` ` ` `// If first occurrence of 1` ` ` `// is found` ` ` `if` `(s.charAt(i) == ` `'1'` `)` ` ` `{` ` ` ` ` `// Mark the index` ` ` `pos = i;` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// Base case: If no 1 occurred` ` ` `if` `(pos == -` `1` `)` ` ` `{` ` ` `// No operations required` ` ` `return` `0` `;` ` ` `}` ` ` `// Stores the character` ` ` `// for which last operation` ` ` `// was performed` ` ` `int` `last = ` `1` `;` ` ` `// Stores minimum number` ` ` `// of operations` ` ` `int` `ans = ` `1` `;` ` ` `// Iterate from pos to n` ` ` `for` `(` `int` `i = pos + ` `1` `; i < n; i++)` ` ` `{` ` ` ` ` `// Check if s[i] is 0` ` ` `if` `(s.charAt(i) == ` `'0'` `)` ` ` `{` ` ` `// Check if last operation was` ` ` `// performed because of 1` ` ` `if` `(last == ` `1` `) ` ` ` `{` ` ` `ans++;` ` ` `// Set last to 0` ` ` `last = ` `0` `;` ` ` `}` ` ` `}` ` ` `else` ` ` `{` ` ` `if` `(last == ` `0` `) ` ` ` `{` ` ` `// Check if last operation was` ` ` `// performed because of 0` ` ` `ans++;` ` ` `// Set last to 1` ` ` `last = ` `1` `;` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// Return the answer` ` ` `return` `ans;` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` `String s = ` `"10111"` `;` ` ` ` ` `System.out.println(minOperations(s));` `}` `}` `// This code is contributed by offbeat` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function to find the minimum` `# number of operations required` `# to obtain the string s` `def` `minOperations(s):` ` ` `n ` `=` `len` `(s)` ` ` `pos ` `=` `-` `1` ` ` `# Iterate the string s` ` ` `for` `i ` `in` `range` `(` `len` `(s)):` ` ` `# If first occurrence of 1` ` ` `# is found` ` ` `if` `(s[i] ` `=` `=` `'1'` `):` ` ` `# Mark the index` ` ` `pos ` `=` `i` ` ` `break` ` ` `# Base case: If no 1 occurred` ` ` `if` `(pos ` `=` `=` `-` `1` `):` ` ` `# No operations required` ` ` `return` `0` ` ` `# Stores the character` ` ` `# for which last operation` ` ` `# was performed` ` ` `last ` `=` `1` ` ` `# Stores minimum number` ` ` `# of operations` ` ` `ans ` `=` `1` ` ` `# Iterate from pos to n` ` ` `for` `i ` `in` `range` `(pos ` `+` `1` `, n):` ` ` `# Check if s[i] is 0` ` ` `if` `(s[i] ` `=` `=` `'0'` `):` ` ` `# Check if last operation was` ` ` `# performed because of 1` ` ` `if` `(last ` `=` `=` `1` `):` ` ` `ans ` `+` `=` `1` ` ` `# Set last to 0` ` ` `last ` `=` `0` ` ` ` ` `else` `:` ` ` `if` `(last ` `=` `=` `0` `):` ` ` `# Check if last operation was` ` ` `# performed because of 0` ` ` `ans ` `+` `=` `1` ` ` `# Set last to 1` ` ` `last ` `=` `1` ` ` `# Return the answer` ` ` `return` `ans` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `s ` `=` `"10111"` ` ` `print` `(minOperations(s))` `# This code is contributed by chitranayal` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement the` `// above approach` `using` `System;` `class` `GFG{` ` ` `// Function to find the minimum` `// number of operations required` `// to obtain the string s` `static` `int` `minOperations(String s)` `{` ` ` `int` `n = s.Length;` ` ` `int` `pos = -1;` ` ` ` ` `// Iterate the string s` ` ` `for` `(` `int` `i = 0; i < s.Length; i++) ` ` ` `{` ` ` ` ` `// If first occurrence of 1` ` ` `// is found` ` ` `if` `(s[i] == ` `'1'` `)` ` ` `{` ` ` ` ` `// Mark the index` ` ` `pos = i;` ` ` `break` `;` ` ` `}` ` ` `}` ` ` ` ` `// Base case: If no 1 occurred` ` ` `if` `(pos == -1)` ` ` `{` ` ` ` ` `// No operations required` ` ` `return` `0;` ` ` `}` ` ` ` ` `// Stores the character` ` ` `// for which last operation` ` ` `// was performed` ` ` `int` `last = 1;` ` ` ` ` `// Stores minimum number` ` ` `// of operations` ` ` `int` `ans = 1;` ` ` ` ` `// Iterate from pos to n` ` ` `for` `(` `int` `i = pos + 1; i < n; i++)` ` ` `{` ` ` ` ` `// Check if s[i] is 0` ` ` `if` `(s[i] == ` `'0'` `)` ` ` `{` ` ` ` ` `// Check if last operation was` ` ` `// performed because of 1` ` ` `if` `(last == 1) ` ` ` `{` ` ` `ans++;` ` ` ` ` `// Set last to 0` ` ` `last = 0;` ` ` `}` ` ` `}` ` ` `else` ` ` `{` ` ` `if` `(last == 0) ` ` ` `{` ` ` ` ` `// Check if last operation was` ` ` `// performed because of 0` ` ` `ans++;` ` ` ` ` `// Set last to 1` ` ` `last = 1;` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// Return the answer` ` ` `return` `ans;` `}` ` ` `// Driver code` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` `String s = ` `"10111"` `;` ` ` ` ` `Console.Write(minOperations(s));` `}` `}` ` ` `// This code is contributed by rock_cool` |

*chevron_right*

*filter_none*

**Output:**

3

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

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:

- Minimum number of steps required to obtain the given Array by the given operations
- Minimize operations required to obtain N
- Count of decrement operations required to obtain K in N steps
- Minimum given operations required to convert a given binary string to all 1's
- Minimum count of digits required to obtain given Sum
- Minimum Steps to obtain N from 1 by the given operations
- Minimum number of given operations required to convert a string to another string
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Minimum increments by index value required to obtain at least two equal Array elements
- Minimum number of operations required to sum to binary string S
- Minimum number of operations required to maximize the Binary String
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimize Steps required to obtain Sorted Order of an Array
- Minimum operations required to make the string satisfy the given condition
- Minimum operations required to convert all characters of a String to a given Character
- Minimum number of given operations required to make two strings equal
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of distinct powers of 2 required to express a given binary number
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Count of Ways to obtain given Sum from the given Array elements

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.