Open In App

Make a given Binary String non-decreasing by removing the smallest subsequence

Given a binary string str of size N, the task is to find the length of the smallest subsequence such that after erasing the subsequence the resulting string will be the longest continuous non-decreasing string.

Example :



Input: str = “10011”
Output: 1
Explanation: Removal of the first occurrence of ‘1’ results in a non-decreasing subsequence, i.e “0011”.

Input: str = “11110000”
Output: 4



Approach: The problem can be solved based on the following observations:

The non-decreasing subsequences can be of the following 3 types:

  • Case 1 : 00000…..
  • Case 2 : 11111…..
  • Case 3 : 0000….111111….

Follow the given steps to solve the problem:

Below is the implementation of the above approach :




// C++ program for
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the
// length of smallest subsequence
// required to be removed to make
// the given string non-decreasing
int min_length(string str)
{
 
    // Length of the string
    int n = str.length();
 
    // Count of zeros and ones
    int total_zeros = 0;
    int total_ones = 0;
 
    // Traverse the string
    for (int i = 0; i < n; i++) {
        if (str[i] == '0')
            total_zeros++;
        else
            total_ones++;
    }
 
    // Count minimum removals to
  // obtain strings of the form
  // "00000...." or "11111..."
    int ans = min(total_zeros, total_ones);
 
    int cur_zeros = 0, cur_ones = 0;
 
    for (char x : str) {
 
        // Increment count
        if (x == '0')
            cur_zeros++;
        else
            cur_ones++;
 
        // Remove 1s and remaining 0s
        ans = min(ans, cur_ones
                  + (total_zeros - cur_zeros));
    }
 
    cout << ans;
}
 
// Driver Code
int main()
{
    string str = "10011";
    min_length(str);
 
    return 0;
}




// Java program for
// the above approach
import java.io.*;
 
class GFG
{
 
  // Function to return the
  // length of smallest subsequence
  // required to be removed to make
  // the given string non-decreasing
  public static void min_length(String str)
  {
 
    // Length of the string
    int n = str.length();
 
    // Count of zeros and ones
    int total_zeros = 0;
    int total_ones = 0;
 
    // Traverse the string
    for (int i = 0; i < n; i++) {
      if (str.charAt(i) == '0'){
        total_zeros++;
      }
      else{
        total_ones++;
      }
    }
 
    // Count minimum removals to
    // obtain strings of the form
    // "00000...." or "11111..."
    int ans = Math.min(total_zeros, total_ones);
    int cur_zeros = 0, cur_ones = 0;
    for (int i = 0; i<str.length(); i++)
    {
 
      // Increment count
      char x = str.charAt(i);
      if (x == '0'){
        cur_zeros++;
      }
      else{
        cur_ones++;
      }
 
      // Remove 1s and remaining 0s
      ans = Math.min(ans, cur_ones
                     + (total_zeros - cur_zeros));
    }
 
    System.out.println(ans);
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    String str = "10011";
    min_length(str);
  }
}
 
// This code is contributed by rohitsingh07052




# Python3 program for
# the above approach
 
# Function to return the
# length of smallest subsequence
# required to be removed to make
# the given string non-decreasing
def min_length(str):
   
    # Length of the string
    n = len(str)
 
    # Count of zeros and ones
    total_zeros = 0
    total_ones = 0
 
    # Traverse the string
    for i in range(n):
        if (str[i] == '0'):
            total_zeros += 1
        else:
            total_ones += 1
 
    # Count minimum removals to
  # obtain strings of the form
  # "00000...." or "11111..."
    ans = min(total_zeros, total_ones)
    cur_zeros = 0
    cur_ones = 0
    for x in str:
       
        # Increment count
        if (x == '0'):
            cur_zeros += 1
        else:
            cur_ones += 1
 
        # Remove 1s and remaining 0s
        ans = min(ans, cur_ones + (total_zeros - cur_zeros))
    print(ans)
 
# Driver Code
if __name__ == '__main__':
    str = "10011"
    min_length(str)
     
    # This code is contributed by SURENDRA_GENGWAR.




// C# program for
// the above approach
using System;
 
class GFG{
     
// Function to return the
// length of smallest subsequence
// required to be removed to make
// the given string non-decreasing
public static void min_length(string str)
{
     
    // Length of the string
    int n = str.Length;
     
    // Count of zeros and ones
    int total_zeros = 0;
    int total_ones = 0;
     
    // Traverse the string
    for(int i = 0; i < n; i++)
    {
        if (str[i] == '0')
        {
            total_zeros++;
        }
        else
        {
            total_ones++;
        }
    }
     
    // Count minimum removals to
    // obtain strings of the form
    // "00000...." or "11111..."
    int ans = Math.Min(total_zeros, total_ones);
    int cur_zeros = 0, cur_ones = 0;
    for(int i = 0; i < str.Length; i++)
    {
         
        // Increment count
        char x = str[i];
        if (x == '0')
        {
            cur_zeros++;
        }
        else
        {
            cur_ones++;
        }
         
        // Remove 1s and remaining 0s
        ans = Math.Min(ans, cur_ones +
                      (total_zeros - cur_zeros));
    }
    Console.WriteLine(ans);
}
 
// Driver code
static public void Main()
{
    string str = "10011";
    min_length(str);
}
}
 
// This code is contributed by offbeat




<script>
 
// Javascript program for
// the above approach
 
// Function to return the
// length of smallest subsequence
// required to be removed to make
// the given string non-decreasing
function min_length(str)
{
 
    // Length of the string
    var n = str.length;
 
    // Count of zeros and ones
    var total_zeros = 0;
    var total_ones = 0;
 
    // Traverse the string
    for (var i = 0; i < n; i++) {
        if (str[i] == '0')
            total_zeros++;
        else
            total_ones++;
    }
 
    // Count minimum removals to
  // obtain strings of the form
  // "00000...." or "11111..."
    var ans = Math.min(total_zeros, total_ones);
 
    var cur_zeros = 0, cur_ones = 0;
 
    for( var i =0; i< str.length; i++){
 
        // Increment count
        if (str[i] == '0')
            cur_zeros++;
        else
            cur_ones++;
 
        // Remove 1s and remaining 0s
        ans = Math.min(ans, cur_ones
                  + (total_zeros - cur_zeros));
    }
 
    document.write( ans);
}
 
// Driver Code
var str = "10011";
min_length(str);
 
</script>

Output
1

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

 


Article Tags :