 GeeksforGeeks App
Open App Browser
Continue

# Minimize count of 0s required to be removed to maximize length of longest substring of 1s

Given a binary string S of length N, the task is to find the minimum number of 0s required to be removed from the given string S to get the longest substring of 1s.

Examples:

Input: S = “010011”
Output: 2
Explanation:
Removing str and str modifies the string S to “0111”. Therefore, the minimum number of removals required is 2.

Input: S = “011111”
Output: 0

Approach: The idea is to find the leftmost and rightmost index of 1 in the string, then count the number of 0s present between them. Finally, print the value of the count obtained. Follow the steps below to solve the problem:

• Traverse the string S to find the first and last occurrence of 1 in the string and store its indices in variables, say left and right, respectively.
• Iterate over the range [left, right] using the variable i, and if the value of str[i] is equal to 0, increment count by 1.
• After completing the above steps, print the value of count as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to count the minimum number``// of 0s required to be removed to``// maximize the longest substring of 1s``void` `minNumZeros(string str)``{``    ``// Stores leftmost and rightmost``    ``// indices consisting of 1``    ``int` `left = INT_MAX, right = INT_MIN;` `    ``// Stores the count of 0s``    ``// between left and right``    ``int` `count = 0;` `    ``// Traverse the string str``    ``for` `(``int` `i = 0; i < str.length(); i++) {` `        ``// If the current character is 1``        ``if` `(str[i] == ``'1'``) {` `            ``// Update leftmost and rightmost``            ``// index consisting of 1``            ``left = min(i, left);``            ``right = max(right, i);``        ``}``    ``}` `    ``// If string consists only of 0s``    ``if` `(left == INT_MAX) {``        ``cout << ``"0"``;``        ``return``;``    ``}` `    ``// Count the number of 0s``    ``// between left and right``    ``for` `(``int` `i = left; i < right; i++) {` `        ``if` `(str[i] == ``'0'``) {``            ``count++;``        ``}``    ``}` `    ``// Print the result``    ``cout << count;``}` `// Driver Code``int` `main()``{``    ``string str = ``"010011"``;``    ``minNumZeros(str);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;` `class` `GFG{` `// Function to count the minimum number``// of 0s required to be removed to``// maximize the longest substring of 1s``static` `void` `minNumZeros(String str)``{``    ` `    ``// Stores leftmost and rightmost``    ``// indices consisting of 1``    ``int` `left = Integer.MAX_VALUE;``    ``int` `right = Integer.MIN_VALUE;` `    ``// Stores the count of 0s``    ``// between left and right``    ``int` `count = ``0``;` `    ``// Traverse the string str``    ``for``(``int` `i = ``0``; i < str.length(); i++)``    ``{``        ` `        ``// If the current character is 1``        ``if` `(str.charAt(i) == ``'1'``)``        ``{``            ` `            ``// Update leftmost and rightmost``            ``// index consisting of 1``            ``left = Math.min(i, left);``            ``right = Math.max(right, i);``        ``}``    ``}` `    ``// If string consists only of 0s``    ``if` `(left == Integer.MAX_VALUE)``    ``{``        ``System.out.print(``"0"``);``        ``return``;``    ``}` `    ``// Count the number of 0s``    ``// between left and right``    ``for``(``int` `i = left; i < right; i++)``    ``{``        ``if` `(str.charAt(i) == ``'0'``)``        ``{``            ``count++;``        ``}``    ``}` `    ``// Print the result``    ``System.out.print(count);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``String str = ``"010011"``;``    ` `    ``minNumZeros(str);``}``}` `// This code is contributed by Dharanendra L V`

## Python3

 `# Python program for the above approach``import` `sys` `# Function to count the minimum number``# of 0s required to be removed to``# maximize the longest substring of 1s``def` `minNumZeros(st) :` `    ``# Stores leftmost and rightmost``    ``# indices consisting of 1``    ``left ``=` `sys.maxsize``    ``right ``=` `-``sys.maxsize ``-` `1` `    ``# Stores the count of 0s``    ``# between left and right``    ``count ``=` `0` `    ``# Traverse the string str``    ``for` `i ``in` `range``(``len``(st)) :``    ``#for (int i = 0; i < str.length(); i++) {` `        ``# If the current character is 1``        ``if` `st[i] ``=``=` `'1'` `:` `            ``# Update leftmost and rightmost``            ``# index consisting of 1``            ``left ``=` `min``(i, left)``            ``right ``=` `max``(right, i)``        ` `    ``# If string consists only of 0s``    ``if` `left ``=``=` `sys.maxsize :``        ``print``(``"0"``)``        ``return``    ` `    ``# Count the number of 0s``    ``# between left and right``    ``for` `i ``in` `range``(left,right):``        ` `    ``#for (int i = left; i < right; i++) {` `        ``if` `st[i] ``=``=` `'0'` `:``            ``count ``+``=` `1``        ` `    ``# Print the result``    ``print``(count)` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:    ``    ``st ``=` `"010011"``;``    ``minNumZeros(st)``    ` ` ``# This code is contributed by jana_sayantan.  `

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to count the minimum number``// of 0s required to be removed to``// maximize the longest substring of 1s``static` `void` `minNumZeros(``string` `str)``{``    ` `    ``// Stores leftmost and rightmost``    ``// indices consisting of 1``    ``int` `left = ``int``.MaxValue;``    ``int` `right = ``int``.MinValue;` `    ``// Stores the count of 0s``    ``// between left and right``    ``int` `count = 0;` `    ``// Traverse the string str``    ``for``(``int` `i = 0; i < str.Length; i++)``    ``{``        ` `        ``// If the current character is 1``        ``if` `(str[i] == ``'1'``)``        ``{``            ` `            ``// Update leftmost and rightmost``            ``// index consisting of 1``            ``left = Math.Min(i, left);``            ``right = Math.Max(right, i);``        ``}``    ``}` `    ``// If string consists only of 0s``    ``if` `(left == ``int``.MaxValue)``    ``{``        ``Console.WriteLine(``"0"``);``        ``return``;``    ``}` `    ``// Count the number of 0s``    ``// between left and right``    ``for``(``int` `i = left; i < right; i++)``    ``{``        ``if` `(str[i] == ``'0'``)``        ``{``            ``count++;``        ``}``    ``}` `    ``// Print the result``    ``Console.WriteLine(count);``}` `// Driver Code``static` `public` `void` `Main()``{``    ``string` `str = ``"010011"``;``    ` `    ``minNumZeros(str);``}``}` `// This code is contributed by Dharanendra L V`

## Javascript

 ``

Output:

`2`

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

My Personal Notes arrow_drop_up