Segregate 1s and 0s in separate halves of a Binary String


In this tutorial, we need to separate all 1s and 0s of the given binary string in the two halves. Here, we need to take a substring from the given string and reverse it to separate 0s and 1s in different parts. Ultimately, we need to count the total number of reversals required for substring to separate 1s and 0s in two halves.

Problem statement − We have given a binary string of even length. We need to take any substring from the given string multiple times and reverse it to separate it into two halves. We need to print the count of the total number of reversals required at the end.

Sample Examples

Input –  str = 0011
Output – 0

Explanation

We need 0 reversal as the string is separated into two halves.

Input –  str = 0011101000
Output – 2

Explanation

  • First, take the substring of length equal to 2 from the 5th index, and reverse it. The resultant string will be 0011110000.

  • After that, take a string of length equal to 6 from the start, and reverse it. The resultant string will be 1111000000

Input –  str = 010101
Output – 2

Explanation

  • Reverse the string of length 2 starting from the 1st index. The resultant string will be 001101.

  • Now, reverse the string of length 3 starting from the 2nd index. The final string will be 000111.

Approach 1

This approach will count the total number of different adjacent elements. After that, we can say that the total number of reversals required is count / 2.

Let’s understand it via debugging the sample input.

Input –  str = 00111010

So, the total number of different adjacent elements is 4. Here, str[1] != str[2], str[4] != str[5], str[5] != str[6], and str[6] != str[7].

So, the count value is 4, and answer is count/2, which equals 2.

Algorithm

  • Step 1 − Initialize the ‘cnt’ variable with 0.

  • Step 2 − Use for loop, and iterate through the string.

  • Step 3 − In the for loop, if the current element is not equal to the previous element, increase the value of the ‘cnt’ variable by 1.

  • Step 4 − If the value of ‘cnt’ is odd, return (cnt -1) /2. Otherwise, return cnt/2.

Example

#include <bits/stdc++.h>
using namespace std;
//  function to find the minimum number of reversals required to segregate 1s and 0s in a separate half
int minimumReversal(string str, int len){
   int cnt = 0; // initialize count with zero
   for (int i = 1; i < len; i++){
   
      // if the adjacent elements are not the same, then the increment count
      if (str[i] != str[i - 1]){
         cnt++;
      }
   }
   
   // For odd count
   if (cnt % 2 == 1){
      return (cnt - 1) / 2;
   }
   return cnt / 2; // For even count
}
int main(){
   string str = "00111010";
   int len = str.size();
   cout << "Minimum number of operations required is : " << minimumReversal(str, len);
   return 0;
}

Output

Minimum number of operations required is : 2

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

Space complexity − O(N), as we use constant space to store counts.

Here, we used the logic that we need to reverse whenever we find two different adjacent elements. Also, in single reverse, we can set two elements so we return count/2 as a resultant value.

Updated on: 28-Jul-2023

168 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements