Related Articles
Make a given Binary String non-decreasing by removing the smallest subsequence
• Difficulty Level : Easy
• Last Updated : 25 Feb, 2021

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:

• Iterate over the characters of the string.
• Count the number of 0s and 1s present in the string
• To generate non-decreasing subsequences of the form “0000….”, minimum removals required is the count of 1s in the string
• To generate non-decreasing subsequences of the form “1111….”, minimum removals required is the count of 0s in the string
• To generate non-decreasing subsequences of the form “0000…1111….”, minimum removals required can be obtained using the following steps:
• Finally, print the minimum removals obtained in the above three cases as the required answer.

Below is the implementation of the above approach :

## C++

 `// C++ program for``// the above approach``#include ``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

 `// 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

## Python3

 `# Python 3 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.`

Output
`1`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up