Maximum Number of 0s Placed Consecutively at the Start and End in any Rotation of a Binary String


Binary string means the string contains only two types of char either 1 or 0. It is known as base 2. In this problem, we have given a binary string str and also the size of the string 'n'. Our task is to find the maximum number of zeros places consecutively at the start and end of any rotation of a binary string. Let's see examples with explanations below to understand the problem in a better way.

Sample Example

Input 1

str = “101001, n = 6

Output 1

2

Explanation

The string can be rotated in any of the following possible ways:

"101001", The number of 0s in the string's beginning is 0, while its ending is 0. Total = 0+0 = 0

"110100", The number of 0s in the string's beginning is 0, while its ending is 2. Total = 0+2 = 2

"011010", The number of 0s in the string's beginning is 1, while its ending is 1. Total = 1+1 = 2

"001101", The number of 0s in the string's beginning is 2, while its ending is 0. Total = 2+0 = 2

"100110", The number of 0s in the string's beginning is 0, while its ending is 1. Total = 0+1 = 1

"010011", The number of 0s in the string's beginning is 1, while its ending is 0. Total = 1+0 = 1

Since the maximum possible zeros are 2.

Input 2

str = “1100”, k = 4

Output 2

2

Approaches

We have seen the example above for the given string of size n, let us move to the approaches:

Approach 1: Naive Approach

The concept is simple to produce all possible rotations of the given string and check each string, count the number of zeros placed consecutively at the begning and ending of the string. Maintain the maximum cout of 0's in the variable 'cnt' and return that count.

Example

Let's see the code below for a better understanding of the above approach.

#include <bits/stdc++.h>
using namespace std;
// Create a function to count maximum consecutive 0's at  the beginning and ending in any rotation of binary string
int maxZeros(string str, int n){
   // Verify that the string contains only 0s as characters.
   int cnt = 0;
   // traverse the string to check all the char of the string are 0's or not
   for (int i = 0; i < n; i++) {
      if (str[i] == '0')
         cnt++;
   }
   // check the count of 0's equal to the size of the string
   if (cnt == n) {
      return cnt;
   }
   string sstr = str + str; //extended string with itself
   cnt = 0; //reset the count equal to 0
   // Create all possible string rotations.
   for (int i = 0; i < n; i++) {
      int cnts = 0; //0's at start
      int cnte = 0; //0's at end
      // for the beginning zeroes 
       for (int j = i; j < i + n; ++j) {
          if (sstr[j]=='0')
             cnts++;
          else
             break;
       } 
       // for the ending zeroes
       for (int j = i + n - 1; j >= i; j--) {
          if (sstr[j]=='0')
             cnte++;
          else
             break;
       } 
       // get maximum 0's
       cnt = max(cnt, cnts+cnte);
   }   
   // return maximum cout of the 0's
      return cnt;
} 
int main(){
   string str = "101001"; // given string
   cout << "Input String: " << str << endl;
   int n = 6; //size of the string 
   int maxZerosCount = maxZeros(str, n);    
   cout << "Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String: ";
   cout<<maxZerosCount; // Print maximum cout of the 0's
   return 0;
}
    

Output

Input String: 101001
Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String: 2

Time and Space Complexity

The time complexity is O(N^2). The space complexity is O(N). Here N is the size of the string

Approach 2: Consecutive zero's

The idea is simple to calculate consecutive zero's for the given string and count the zeros placed consecutively in the string at the start and end. Maintain the maximum cout of 0's in the variable 'cnt' and return that count.

Example

Let's see the code below for a better understanding of the above approach.

#include <bits/stdc++.h>
using namespace std; 
// Create a function to count maximum consecutive 0's at the beginning and ending in any rotation of binary string
int maxZeros(string str, int n){
   // Verify that the string contains only 0s as characters.
   int cnt = 0;    
   // Traverse string to check if all the chars of the string are 0's or not
   for (int i = 0; i < n; i++) {
      if (str[i]=='0')
         cnt++;
   } 
   // check the count of 0's equal to the size of the string
   if (cnt == n) {
       return cnt;
   }
   cnt = 0; //reset the count equal to 0 
   int tempcnt = 0; 
   for (int i = 0; i < n; i++) {
      if (str[i]=='0')
         tempcnt++;
      else {
         cnt = max(cnt, tempcnt);
         tempcnt = 0;
      }
   } 
    // get maximum count 
    cnt = max(cnt, tempcnt);
    // For 0's present at the end and 
    // beginning of the string
    int s = 0, e = n - 1;
    tempcnt = 0; 
    // for start
    while (str[s] != '1' && s < n) {
       tempcnt++;
       s++;
    }
    // for end
    while (str[e] != '1' && e >= 0) {
       tempcnt++;
       e--;
    } 
    // get maximum 0's
    cnt = max(cnt, tempcnt);
    // return maximum cout of the 0's
    return cnt; 
 } 
 int main(){
    string str = "101001"; // given string
    cout << "Input String: " << str << endl;
    int n = 6; //size of the string
    int maxZerosCount = maxZeros(str, n);
    cout << "Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String: ";
    cout<<maxZerosCount; // print maximum cout of the 0's 
    return 0;
}

Output

Input String: 101001
Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String: 2

Time and Space Complexity

The time complexity: O(N), Where N is the size of the string. The space complexity: O(1)

Conclusion

In this tutorial, we have implemented a program to find the Maximum number of 0s placed consecutively at the start and end of any rotation of a Binary String. We have implemented two approaches: naive and efficient. Naive approach with time complexity O(N^2) and space complexity O(N). Efficient approach with time complexity O(N) and space complexity O(1).

Updated on: 11-Jul-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements