Longest Non-Increasing Subsequence in a Binary String


In this problem, we require to find the longest non-increasing subsequence of the given string.

The meaning of non-increasing is either character should be the same or in decreasing order. As binary string contains only ‘0’ and ‘1’, the resultant string should start with ‘1’ and end with ‘0’, or start and end with either ‘0’ or ‘1’.

To solve the problem, we will count prefix ‘1’s and suffix ‘0’ at each position of the string and find the maximum sum of prefix ‘1’s and suffix ‘0’s.

Problem statement − We have given binary string str. We need to find the longest non-increasing subsequence from the given string.

Sample Examples

Input –  str = "010100"
Output – 4

Explanation

The longest non-increasing subsequence is ‘1100’.

Input –  str = "010110010"
Output – 6

Explanation

The longest non-increasing subsequence is ‘111000’.

Input –  str = ‘00000000’
Output – 8

Explanation

The longest non-increasing subsequence is ‘00000000’, which is equal to the given string.

Approach 1

In this approach, we will store the count of the prefix ‘1’s and suffixes ‘0’s in the array for every index. After that, we get values from the same index in both arrays, sum them, and find the maximum sum.

Algorithm

  • Step 1 − Define the pre1s and suffix0s array to store prefix 1s and suffix 0s. Also, initialize all array elements with 0.

  • Step 2 − Use the for loop to traverse the string and count prefix 1s for each index. If I > 0, add the value of the previous element to the current element.

  • Step 3 − If the current character is ‘1’, add 1 to the current value of the pre1s[i].

  • Step 4 − Now, calculate the suffix 0s in the given string. Start traversing the string from the end.

  • Step 5 − If the value of ‘I’ is not equal to the ‘n – 1’, get the value of the ‘I + 1’ element and add it to the current element.

  • Step 6 − If the current element is ‘0’, add 1 to the current element.

  • Step 7 − Now, initialize the ‘res’ variable with 0.

  • Step 8 − Iterate through the ‘pre1s’ and ‘suffix0s’ using the loop.

  • Step 9 − Get the value from the ith index in the ‘pre1s’ and ‘suffix0s’ array, and add them. Also, change the ‘res’ variables value with the ‘sum’ value if ‘sum is greater than the current value of the ‘res’ variable.

  • Step 10 − Return the value of ‘res’ variable.

Example

For the input ‘010100’, the prefix array will be [0, 1, 1, 2, 2, 2], and the suffix 0’s array will be [4, 3, 3, 2, 2, 1]. The sum array will be [4, 4, 4, 4, 4, 1], and the maximum value in the sum array is 4. So, the answer will be 4.

#include <bits/stdc++.h>
using namespace std;
int getMaxLength(string str, int n){
   // To store the prefix count of '1's and suffix count of '0's
   int pre1s[n] = {0},
      suffix0s[n] = {0};
   for (int i = 0; i < n; i++){
   
      // get the prefix count of '1's
      if (i > 0){
         pre1s[i] += pre1s[i - 1];
      }
      
      // If the current character is '1', then update the pre1s array by adding 1; else, keep it as it is.
      if (str[i] == '1'){
         pre1s[i] += 1;
      }
   }
   
   // get suffix count of '0's
   for (int i = n - 1; i >= 0; i--) {
   
      // add the suffix count of '0's
      if (i != n - 1)
         suffix0s[i] += suffix0s[i + 1];
         
      // If the current character is '0', then update the suffix0s array by adding 1; else, keep it as it is.
      if (str[i] == '0')
         suffix0s[i] += 1;
   }
   
   // to store the final result value
   int res = 0;
   
   // iterate over the pre1s[] and suffix0s[] array and find the maximum value of pre1s[i] + suffix0s[i]
   for (int i = 0; i < n; i++){
      res = max(res, pre1s[i] + suffix0s[i]);
   }
   
   // Return the result
   return res;
}

// Driver Code
int main(){
   string str = "010100";
   int N = str.length();
   cout << "The length of the longest non-increasing subsequence in the given binary string is - " << getMaxLength(str, N);
   return 0;
}

Output

The length of the longest non-increasing subsequence in the given binary string is - 4

Time complexity − O(N), as we need to initialize the prefix 1s and suffix 0s array.

Space complexity − O(N), as we store the prefix 1s and suffix 0s in the array.

Approach 2

In this approach, we will count a total number of zeros initially. After that, we start traversing the string and keep counting ‘1’s and decreasing ‘0’s if we find 0. Also, we take the sum of counts of zero and one in each iteration and find the maximum resultant value.

Algorithm

  • Step 1 − Define the ‘count1’, ‘count0’, and ‘res’ variables and initialize them with 0 to store the count of 1s, 0s, and final results, respectively.

  • Step 2 − Count a total number of zeros by traversing the string and storing it in the ‘count0’ variable.

  • Step 3 − Now, use the loop to iterate the string.

  • Step 4 − In the loop, if the current character is ‘1’, increase the value of the ‘count1’ by 1, else decrease the value of the ‘count0’ by 1.

  • Step 5 − Also, store the maximum value from the ‘res’ and ‘count0 + count1’ into the ‘res’ variable.

  • Step 6 − When the loop terminates, return the value of the ‘res’ variable.

Example

#include <bits/stdc++.h>
using namespace std;
int getMaxLength(string str, int n){
   int count1 = 0, count0 = 0;
   int res = 0;
   // counting total zeros in the string
   for (int i = 0; i < n; i++){
      if (str[i] == '0')
         count0++;
   }
   
   // counting 1s from left, subtracting zeros from total zeros and adding both counts.
   for (int i = 0; i < n; i++){
      if (str[i] == '1')
         count1++;
      else
         count0--;
      res = max(res, count1 + count0);
   }
   return res;
}
int main(){
   string str = "010110010";
   int N = str.length();
   cout << "The length of the longest non-increasing subsequence in the given binary string is - " << getMaxLength(str, N);
   return 0;
}

Output

The length of the longest non-increasing subsequence in the given binary string is - 6

Time complexity − O(N), as we count the total number of zeros in the string and traverse the string to find the longest subsequence.

Space complexity − O(1)

Conclusion

Here, both approaches have the same time complexity but different space complexity. The second approach uses the constant space as we optimized the code, but the first uses the dynamic space to store the total number of prefix 1s and suffix 0s.

Updated on: 28-Jul-2023

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements