Minimize count of 0s required to be removed to maximize length of longest substring of 1s
Last Updated :
05 Jul, 2021
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[2] and str[3] 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++
#include <bits/stdc++.h>
using namespace std;
void minNumZeros(string str)
{
int left = INT_MAX, right = INT_MIN;
int count = 0;
for ( int i = 0; i < str.length(); i++) {
if (str[i] == '1' ) {
left = min(i, left);
right = max(right, i);
}
}
if (left == INT_MAX) {
cout << "0" ;
return ;
}
for ( int i = left; i < right; i++) {
if (str[i] == '0' ) {
count++;
}
}
cout << count;
}
int main()
{
string str = "010011" ;
minNumZeros(str);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void minNumZeros(String str)
{
int left = Integer.MAX_VALUE;
int right = Integer.MIN_VALUE;
int count = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
if (str.charAt(i) == '1' )
{
left = Math.min(i, left);
right = Math.max(right, i);
}
}
if (left == Integer.MAX_VALUE)
{
System.out.print( "0" );
return ;
}
for ( int i = left; i < right; i++)
{
if (str.charAt(i) == '0' )
{
count++;
}
}
System.out.print(count);
}
public static void main(String[] args)
{
String str = "010011" ;
minNumZeros(str);
}
}
|
Python3
import sys
def minNumZeros(st) :
left = sys.maxsize
right = - sys.maxsize - 1
count = 0
for i in range ( len (st)) :
if st[i] = = '1' :
left = min (i, left)
right = max (right, i)
if left = = sys.maxsize :
print ( "0" )
return
for i in range (left,right):
if st[i] = = '0' :
count + = 1
print (count)
if __name__ = = "__main__" :
st = "010011" ;
minNumZeros(st)
|
C#
using System;
class GFG{
static void minNumZeros( string str)
{
int left = int .MaxValue;
int right = int .MinValue;
int count = 0;
for ( int i = 0; i < str.Length; i++)
{
if (str[i] == '1' )
{
left = Math.Min(i, left);
right = Math.Max(right, i);
}
}
if (left == int .MaxValue)
{
Console.WriteLine( "0" );
return ;
}
for ( int i = left; i < right; i++)
{
if (str[i] == '0' )
{
count++;
}
}
Console.WriteLine(count);
}
static public void Main()
{
string str = "010011" ;
minNumZeros(str);
}
}
|
Javascript
<script>
function minNumZeros(str)
{
let left = Number.MAX_VALUE, right = Number.MIN_VALUE;
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] == '1' ) {
left = Math.min(i, left);
right = Math.max(right, i);
}
}
if (left == Number.MAX_VALUE) {
document.write( "0" );
return ;
}
for (let i = left; i < right; i++) {
if (str[i] == '0' ) {
count++;
}
}
document.write(count);
}
let str = "010011" ;
minNumZeros(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...