Minimum count of 0s to be removed from given Binary string to make all 1s occurs consecutively
Given a binary string S of size N, the task is to find the minimum numbers of 0s that must be removed from the string S such that all the 1s occurs consecutively.
Examples:
Input: S = “010001011”
Output: 4
Explanation:
Removing the characters { S[2], S[3], S[4], S[6] } from the string S modifies the string S to “01111”.
Therefore, the required output is 4.
Input: S = “011110000”
Output: 0
Explanation:
All 1s in S already group together, therefore, the required output is 0.
Approach: The given problem can be solved by observing the fact that removing leading and ending 0s in the string doesn’t minimizes the count of 0s that must be removed. Therefore, the idea is to find the first and the last occurrence of 1 in the string S and find the count of 0s between them. Follow the steps below to solve the problem:
- Iterate over the characters of the string, S from left to right and find the index of first occurrence of 1s in the given string say X.
- Traverse the string from right to left and find the index of last occurrence of 1s in the given string say Y.
- After completing the above steps, print the count of 0s between the index X and Y as the result.
C++
#include <bits/stdc++.h>
using namespace std;
void makeContiguous(string S, int N)
{
int fst_occur, lst_occur;
for ( int x = 0; x < N; x++) {
if (S[x] == '1' ) {
fst_occur = x;
break ;
}
}
for ( int x = N - 1; x >= 0; x--) {
if (S[x] == '1' ) {
lst_occur = x;
break ;
}
}
int count = 0;
for ( int x = fst_occur;
x <= lst_occur; x++) {
if (S[x] == '0' ) {
count++;
}
}
cout << count;
}
int main()
{
string S = "010001011" ;
int N = S.size();
makeContiguous(S, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void makeContiguous(String S, int N)
{
int fst_occur= 0 , lst_occur= 0 ;
for ( int x = 0 ; x < N; x++) {
if (S.charAt(x) == '1' ) {
fst_occur = x;
break ;
}
}
for ( int x = N - 1 ; x >= 0 ; x--) {
if (S.charAt(x) == '1' ) {
lst_occur = x;
break ;
}
}
int count = 0 ;
for ( int x = fst_occur; x <= lst_occur; x++) {
if (S.charAt(x) == '0' ) {
count++;
}
}
System.out.print(count);
}
public static void main(String[] args)
{
String S = "010001011" ;
int N = S.length();
makeContiguous(S, N);
}
}
|
Python3
def makeContiguous(S, N):
fst_occur = 0
lst_occur = 0
for x in range (N):
if (S[x] = = '1' ):
fst_occur = x
break
x = N - 1
while (x > = 0 ):
if (S[x] = = '1' ):
lst_occur = x
break
x - = 1
count = 0
for x in range (fst_occur,lst_occur + 1 , 1 ):
if (S[x] = = '0' ):
count + = 1
print (count)
if __name__ = = '__main__' :
S = "010001011"
N = len (S)
makeContiguous(S, N)
|
C#
using System;
class GFG{
static void makeContiguous( string S, int N)
{
int fst_occur = 0, lst_occur = 0;
for ( int x = 0; x < N; x++)
{
if (S[x] == '1' )
{
fst_occur = x;
break ;
}
}
for ( int x = N - 1; x >= 0; x--)
{
if (S[x] == '1' )
{
lst_occur = x;
break ;
}
}
int count = 0;
for ( int x = fst_occur; x <= lst_occur; x++)
{
if (S[x] == '0' )
{
count++;
}
}
Console.Write(count);
}
public static void Main()
{
string S = "010001011" ;
int N = S.Length;
makeContiguous(S, N);
}
}
|
Javascript
<script>
function makeContiguous( S , N)
{
var fst_occur = 0, lst_occur = 0;
for ( var x = 0; x < N; x++) {
if (S.charAt(x) == '1' ) {
fst_occur = x;
break ;
}
}
for ( var x = N - 1; x >= 0; x--) {
if (S.charAt(x) == '1' ) {
lst_occur = x;
break ;
}
}
var count = 0;
for (x = fst_occur; x <= lst_occur; x++) {
if (S.charAt(x) == '0' ) {
count++;
}
}
document.write(count);
}
var S = "010001011" ;
var N = S.length;
makeContiguous(S, N);
</script>
|
Time complexity: O(N) where N is the length of the given binary string
Auxiliary space: O(1), as constant extra space is required
Last Updated :
11 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...