Check if a Binary String can be sorted in decreasing order by removing non-adjacent characters


In this problem, we need to sort the given binary string in decreasing order by removing only non-adjacent elements.

To solve the problem, we require to remove all zeros which are placed before ones in the binary string. If we find two consecutive ones after two consecutive zeros at any position in the string, it means we can’t sort the string in decreasing order. Otherwise, we can sort it out in each case.

Problem statement − We have given binary string str with a length equal to N. We need to check whether we can sort the given string in decreasing order by removing the multiple non-adjacent characters from the given string. If the string can be sorted in decreasing order, print ‘yes’; else, print ‘no’.

Sample Examples

Input –  str = "10101100"
Output – “YES”

Explanation

We can remove ‘0’ from the 2nd and 4th position to sort the string in decreasing order.

Input –  str = "11000"
Output – “YES”

Explanation

The string is already sorted.

Input –  str = “010001100”
Output – “NO”

Explanation

Here, we need to remove 0’s from the 1st, 3rd, 4th, and 5th positions to sort the string, but we can’t remove the adjacent zeros. Also, we can sort the string by removing all ‘1’s, but again it is not possible as two ‘1’s are adjacent.

Approach 1

In this approach, we will start traversing the string from the end. If we find two consecutive ‘1’s, break the loop. After that, we check if the string contains two consecutive ‘0’s. If yes, we return false. Otherwise, we return true.

Algorithm

  • Step 1 − Start to traverse the string using for loop from ‘len – 2’ to 0. Here, ‘len’ is the length of the given binary string.

  • Step 2 − If str[i] and str[i+1] both are equal to ‘1’, terminate the loop using the ‘break’ keyword.

  • Step 3 − Now, start traversing the string from the ith index.

  • Step 4 − If str[j] and str[j+1] both are equal to ‘0’, return 0. If the loop terminates successfully, return 1.

  • Step 5 − Print ‘YES’ or ‘NO’ in the driver code based on the returned value from the isSortPossible() function.

Example

#include <bits/stdc++.h>
using namespace std;
// removing the non-adjacent characters from the given string to make it sorted in descending order
bool isSortPossible(string str, int len){
   int i, j;
   
   // Traverse the string str from the end
   for (i = len - 2; i >= 0; i--){
   
      // if str[i] and str[i + 1] is equal to 1 then break the loop.
      if (str[i] == '1' && str[i + 1] == '1'){
         break;
      }
   }
   
   // start traversing the string from i
   for (int j = i; j >= 0; j--){
      // If str[j] and str[j + 1] is equal to 0 then return false
      if (str[j] == '0' && str[j + 1] == '0'){
         return 0;
      }
   }
   return 1;
}
int main(){
   string str = "10101100";
   int len = str.length();
   cout << "The sorting of the given binary string is possible by removing non-adjacent characters - " << endl;
   if (isSortPossible(str, len))
      cout << "YES" << endl;
   else
      cout << "NO" << endl;

   return 0;
}

Output

The sorting of the given binary string is possible by removing non-adjacent characters −
YES

Time complexity − O(N), as we iterate through the string.

Space complexity − O(1)

Approach 2

In this approach, we will use the same logic as in the first approach, but we have optimized the code to make it more readable. Here, we will use only a single loop rather than using two separate loops to detect two consecutive ‘1’s after two consecutive ‘0’s.

Algorithm

  • Step 1 − Define the ‘isTwoZeros’ variable and initialize with the ‘false’ value.

  • Step 2 − Start to iterate the string from the 0th index to ‘len – 1’.

  • Step 3 − If str[i] and str[I + 1] are ‘0’ and ‘isTwoZeros’ is equal to false, change the value of ‘isTwoZeros’ to true. It means we got two consecutive zeros in the given string.

  • Step 4 − In the else part, if str[i] and str[I + 1] are ‘1’ and ‘isTwoZeros’ is equal to true, return false from the function. It means we got two consecutive ‘1’s after two consecutive zeros.

  • Step 5 − Return true when all iterations of for loop terminates.

Example

#include <bits/stdc++.h>
using namespace std;
// removing the non-adjacent characters from the given string to make it sorted in descending order
bool isSortPossible(string str, int len){
   // to track if there are two adjacent zeros in the string
   bool isTwoZeros = false;
   
   // traverse the string
   for (int i = 0; i < len - 1; i++){
   
      // if two zeros are adjacent, then change the value of isTwoZeros to true
      if (str[i] == '0' && str[i + 1] == '0' && !isTwoZeros){
         isTwoZeros = true;
      }
      else{
      
         // if we find two adjacent ones after finding two adjacent zeros, then return false
         if (str[i] == '1' && str[i + 1] == '1' && isTwoZeros){
            return false;
         }
      }
   }
   
   // return true if we don't find two adjacent ones after finding two adjacent zeros
   return true;
}
int main(){
   string str = "101001100";
   int len = str.length();
   cout << "The sorting of the given binary string is possible by removing non-adjacent characters - " << endl;
   if (isSortPossible(str, len))
      cout << "YES" << endl;
   else
      cout << "NO" << endl;

   return 0;
}

Output

The sorting of the given binary string is possible by removing non-adjacent characters - 
NO

Time complexity − O(N)

Space complexity − O(1)

Conclusion

We learned two approaches to sort the binary string in decreasing order by removing only non-adjacent characters. Both approaches use the same logic with minimal changes in the code. The code of the second approach is more readable than the first approach’s code.

Updated on: 28-Jul-2023

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements