Given circular binary string **S** of size **N**, the task is to count the minimum number of consecutive **0**s required to be removed such that the string contains only **1**s.

A

circular stringis a string whose first and last characters are considered to be adjacent to each other.

**Examples:**

Input:S = “11010001”Output:2Explanation:

Remove the substring {S[2]}. Now, the string modifies to “1110001”.

Remove the substring {S[3], …, S[5]} of consecutive 0s. Now, the string modifies to “1111”.

Therefore, the minimum count of removals required is 2.

Input:S = “00110000”Output:1

**Approach:** The idea to solve the given problem is to traverse the given string and count the number of substrings having the same number of **0**s, say **C**. Now if the first and the last characters of the string are **‘0’**, then print the value of **(C – 1)** as the minimum number of removals required. Otherwise, print the value of **C** as the result.

**Note:** If the given string contains all **0s**, then the minimum number of removals required is **1**. Consider this case separately.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count minimum number of` `// removal of consecutive 0s required to` `// make binary string consists only of 1s` `int` `minRemovals(string str, ` `int` `N)` `{` ` ` `// Stores the count of removals` ` ` `int` `ans = 0;` ` ` `bool` `X = ` `false` `;` ` ` `// Traverse the string S` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// If the current character is '0'` ` ` `if` `(str[i] == ` `'0'` `) {` ` ` `ans++;` ` ` `// Traverse until consecutive` ` ` `// characters are only '0's` ` ` `while` `(str[i] == ` `'0'` `) {` ` ` `i++;` ` ` `}` ` ` `}` ` ` `else` `{` ` ` `X = ` `true` `;` ` ` `}` ` ` `}` ` ` `// If the binary string only` ` ` `// contains 1s, then return 1` ` ` `if` `(!X)` ` ` `return` `1;` ` ` `// If the first and the last` ` ` `// characters are 0` ` ` `if` `(str[0] == ` `'0'` ` ` `and str[N - 1] == ` `'0'` `) {` ` ` `ans--;` ` ` `}` ` ` `// Return the resultant count` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `string S = ` `"11010001"` `;` ` ` `int` `N = S.size();` ` ` `cout << minRemovals(S, N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;` `class` `GFG{` `// Function to count minimum number of` `// removal of consecutive 0s required to` `// make binary string consists only of 1s` `static` `int` `minRemovals(String str, ` `int` `N)` `{` ` ` ` ` `// Stores the count of removals` ` ` `int` `ans = ` `0` `;` ` ` `boolean` `X = ` `false` `;` ` ` `// Traverse the string S` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++)` ` ` `{` ` ` ` ` `// If the current character is '0'` ` ` `if` `(str.charAt(i) == ` `'0'` `)` ` ` `{` ` ` `ans++;` ` ` `// Traverse until consecutive` ` ` `// characters are only '0's` ` ` `while` `(i < N && str.charAt(i) == ` `'0'` `)` ` ` `{` ` ` `i++;` ` ` `}` ` ` `}` ` ` `else` ` ` `{` ` ` `X = ` `true` `;` ` ` `}` ` ` `}` ` ` `// If the binary string only` ` ` `// contains 1s, then return 1` ` ` `if` `(!X)` ` ` `return` `1` `;` ` ` `// If the first and the last` ` ` `// characters are 0` ` ` `if` `(str.charAt(` `0` `) == ` `'0'` `&&` ` ` `str.charAt(N - ` `1` `) == ` `'0'` `)` ` ` `{` ` ` `ans--;` ` ` `}` ` ` `// Return the resultant count` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `String S = ` `"11010001"` `;` ` ` `int` `N = S.length();` ` ` `System.out.println(minRemovals(S, N));` `}` `}` `// This code is contributed by Kingash` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG {` ` ` `// Function to count minimum number of` ` ` `// removal of consecutive 0s required to` ` ` `// make binary string consists only of 1s` ` ` `static` `int` `minRemovals(` `string` `str, ` `int` `N)` ` ` `{` ` ` ` ` `// Stores the count of removals` ` ` `int` `ans = 0;` ` ` `bool` `X = ` `false` `;` ` ` `// Traverse the string S` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// If the current character is '0'` ` ` `if` `(str[i] == ` `'0'` `) {` ` ` `ans++;` ` ` `// Traverse until consecutive` ` ` `// characters are only '0's` ` ` `while` `(str[i] == ` `'0'` `) {` ` ` `i++;` ` ` `}` ` ` `}` ` ` `else` `{` ` ` `X = ` `true` `;` ` ` `}` ` ` `}` ` ` `// If the binary string only` ` ` `// contains 1s, then return 1` ` ` `if` `(!X)` ` ` `return` `1;` ` ` `// If the first and the last` ` ` `// characters are 0` ` ` `if` `(str[0] == ` `'0'` `&& str[N - 1] == ` `'0'` `) {` ` ` `ans--;` ` ` `}` ` ` `// Return the resultant count` ` ` `return` `ans;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `string` `S = ` `"11010001"` `;` ` ` `int` `N = S.Length;` ` ` `Console.Write(minRemovals(S, N));` ` ` `}` `}` `// This code is contributed by subham348.` |

**Output:**

2

**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.