Open In App

Minimum count of 0s to be removed from given Binary string to make all 1s occurs consecutively

Improve
Improve
Like Article
Like
Save
Share
Report

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:
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:
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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum count of
// 0s removed from the string S such
// that all 1s occurs consecutively
void makeContiguous(string S, int N)
{
    // Stores the first and the last
    // occurrence of 1
    int fst_occur, lst_occur;
 
    // Iterate over the characters
    // the string, S
    for (int x = 0; x < N; x++) {
 
        // If current character
        // is '1'
        if (S[x] == '1') {
 
            // Update fst_occur
            fst_occur = x;
            break;
        }
    }
 
    // Iterate over the characters
    // the string, S
    for (int x = N - 1; x >= 0; x--) {
 
        // If current character
        // is '1'
        if (S[x] == '1') {
 
            // Update lst_occur
            lst_occur = x;
            break;
        }
    }
 
    // Stores the count of 0s between
    // fst_occur and lst_occur
    int count = 0;
 
    // Iterate over the characters of S
    // between fst_occur and lst_occur
    for (int x = fst_occur;
        x <= lst_occur; x++) {
 
        // If current character is '0'
        if (S[x] == '0') {
 
            // Update count
            count++;
        }
    }
 
    // Print the resultant minimum count
    cout << count;
}
 
// Driver Code
int main()
{
    string S = "010001011";
    int N = S.size();
    makeContiguous(S, N);
 
    return 0;
}


Java




// Java program for the above approach
 
import java.util.*;
 
class GFG{
 
// Function to find minimum count of
// 0s removed from the String S such
// that all 1s occurs consecutively
static void makeContiguous(String S, int N)
{
    // Stores the first and the last
    // occurrence of 1
    int fst_occur=0, lst_occur=0;
 
    // Iterate over the characters
    // the String, S
    for (int x = 0; x < N; x++) {
 
        // If current character
        // is '1'
        if (S.charAt(x) == '1') {
 
            // Update fst_occur
            fst_occur = x;
            break;
        }
    }
 
    // Iterate over the characters
    // the String, S
    for (int x = N - 1; x >= 0; x--) {
 
        // If current character
        // is '1'
        if (S.charAt(x) == '1') {
 
            // Update lst_occur
            lst_occur = x;
            break;
        }
    }
 
    // Stores the count of 0s between
    // fst_occur and lst_occur
    int count = 0;
 
    // Iterate over the characters of S
    // between fst_occur and lst_occur
    for (int x = fst_occur; x <= lst_occur; x++) {
 
        // If current character is '0'
        if (S.charAt(x) == '0') {
 
            // Update count
            count++;
        }
    }
 
    // Print the resultant minimum count
    System.out.print(count);
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "010001011";
    int N = S.length();
    makeContiguous(S, N);
 
}
}
 
// This code is contributed by gauravrajput1


Python3




# Python3 program for the above approach
 
# Function to find minimum count of
# 0s removed from the string S such
# that all 1s occurs consecutively
def makeContiguous(S, N):
   
    # Stores the first and the last
    # occurrence of 1
    fst_occur = 0
    lst_occur = 0
 
    # Iterate over the characters
    # the string, S
    for x in range(N):
       
        # If current character
        # is '1'
        if (S[x] == '1'):
           
            # Update fst_occur
            fst_occur = x
            break
 
    # Iterate over the characters
    # the string, S
    x = N - 1
    while(x >= 0):
       
        # If current character
        # is '1'
        if (S[x] == '1'):
           
            # Update lst_occur
            lst_occur = x
            break
        x -= 1
 
    # Stores the count of 0s between
    # fst_occur and lst_occur
    count = 0
 
    # Iterate over the characters of S
    # between fst_occur and lst_occur
    for x in range(fst_occur,lst_occur+1,1):
        # If current character is '0'
        if (S[x] == '0'):
            # Update count
            count += 1
 
    # Print the resultant minimum count
    print(count)
 
# Driver Code
if __name__ == '__main__':
    S = "010001011"
    N = len(S)
    makeContiguous(S, N)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find minimum count of
// 0s removed from the string S such
// that all 1s occurs consecutively
static void makeContiguous(string S, int N)
{
     
    // Stores the first and the last
    // occurrence of 1
    int fst_occur = 0, lst_occur = 0;
 
    // Iterate over the characters
    // the string, S
    for(int x = 0; x < N; x++)
    {
         
        // If current character
        // is '1'
        if (S[x] == '1')
        {
             
            // Update fst_occur
            fst_occur = x;
            break;
        }
    }
 
    // Iterate over the characters
    // the string, S
    for(int x = N - 1; x >= 0; x--)
    {
         
        // If current character
        // is '1'
        if (S[x] == '1')
        {
             
            // Update lst_occur
            lst_occur = x;
            break;
        }
    }
 
    // Stores the count of 0s between
    // fst_occur and lst_occur
    int count = 0;
 
    // Iterate over the characters of S
    // between fst_occur and lst_occur
    for(int x = fst_occur; x <= lst_occur; x++)
    {
         
        // If current character is '0'
        if (S[x] == '0')
        {
             
            // Update count
            count++;
        }
    }
 
    // Print the resultant minimum count
    Console.Write(count);
}
 
// Driver Code
public static void Main()
{
    string S = "010001011";
    int N = S.Length;
     
    makeContiguous(S, N);
}
}
 
// This code is contributed by sanjoy_62


Javascript




<script>
// javascript program for the above approach   
// Function to find minimum count of
    // 0s removed from the String S such
    // that all 1s occurs consecutively
    function makeContiguous( S , N)
    {
     
        // Stores the first and the last
        // occurrence of 1
        var fst_occur = 0, lst_occur = 0;
 
        // Iterate over the characters
        // the String, S
        for (var x = 0; x < N; x++) {
 
            // If current character
            // is '1'
            if (S.charAt(x) == '1') {
 
                // Update fst_occur
                fst_occur = x;
                break;
            }
        }
 
        // Iterate over the characters
        // the String, S
        for (var x = N - 1; x >= 0; x--) {
 
            // If current character
            // is '1'
            if (S.charAt(x) == '1') {
 
                // Update lst_occur
                lst_occur = x;
                break;
            }
        }
 
        // Stores the count of 0s between
        // fst_occur and lst_occur
        var count = 0;
 
        // Iterate over the characters of S
        // between fst_occur and lst_occur
        for (x = fst_occur; x <= lst_occur; x++) {
 
            // If current character is '0'
            if (S.charAt(x) == '0') {
 
                // Update count
                count++;
            }
        }
 
        // Print the resultant minimum count
        document.write(count);
    }
 
    // Driver Code
     
        var S = "010001011";
        var N = S.length;
        makeContiguous(S, N);
 
// This code is contributed by umadevi9616
</script>


Output

4

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
Previous
Next
Share your thoughts in the comments
Similar Reads