C++ Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String


In this problem, we need to find the maximum consecutive zeros at the start and end of string rotation. We can follow two approaches to solve the problem. The first approach is to find all rotations of the given string and count start and end zeros. The second approach is to count the maximum consecutive zeros in the string and get the answer.

Problem statement – We have given the binary string named str of size equal to ‘len’. We need to count the maximum number of consecutive zeros at the start and end of any rotation of the string.

Sample examples

Input

str = ‘101’

Output

1

Explanation – Let’s calculate the sum of zeros at the start and end in the each rotation of the string.

  • 101 – Zeros at start = 0, zeros at end = 0, sum = 0

  • 011 – Zeros at start = 1, zeros at end = 0, sum = 1

  • 110 – zeros at start = 0, zeros at end = 1, sum = 1

The maximum sun is 1 from all sums.

Input

str = ‘111111111111111’

Output

0

Explanation -As a string doesn’t contain 0, any rotations of a string can’t have zero at the start or end.

Input

str = ‘110000010’

Output

5

Explanation

  • 110000010 – sum = 1.

  • 100000101 - sum = 0.

  • 000001011 - sum = 5.

  • 000010110 – sum =5.

  • 000101100 – sum =5.

  • 001011000 – sum =5.

  • 010110000 – sum =5.

  • 101100000 – sum =5.

  • 011000001 – sum =1.

The maximum sum among all sums is 5.

Approach 1

In this approach, we will find each rotation of the binary string. After that, we will calculate the sum of starting and ending zeros. We will print the maximum sun value in the output.

Algorithm

Step 1 – Initialize the ‘count0’ variable to store the total number of zeros in the given string.

Step 2 – Now, we need to count the total number of zeros in the given string. Use the loop to traverse the string, and if the current character is ‘0’, increase the count0 value by 1.

Step 3 – If count0 and the length of the string is equal, it means the string contains only zeros. So, return the length of the string.

Step 4 – Now, merge the binary string to itself.

Step 5 – Next, initialize the ‘maximumZeros’ variable with zero to store the maximum sum of zeros at the start and end.

Step 6 – Start traversing the string and define the startZeros and endZeros variables.

Step 7 – Use the loop and find starting consecutive zeros.

Step 8 – After that, find the consecutive ending zeros in the string rotation.

Step 9 – Take the sum of startZeros and endZeros. Update the value of maximumZeros if it is less than the sum of start and end zeros.

Step 10 – Return maximumZeros variable value.

Example

#include <bits/stdc++.h>
using namespace std;

int maxStartEndZeros(string str, int len){
    // To store the total count of zeros in the string
    int count0 = 0;
    // Traverse binary string
    for (int p = 0; p < len; ++p){
        if (str[p] == '0')
            count0++;
    }
    // If the string contains all zeros, return len
    if (count0 == len){
        return len;
    }
    // Merge string
    str = str + str;
    // to store maximum zeros at the start and end
    int maximumZeros = 0;
    // Traverse string
    for (int p = 0; p < len; ++p){
        // variables to store the count of zeros at the start and end
        int startZeros = 0;
        int endZeros = 0;
        // Get starting zeros in the current rotation
        for (int q = p; q < p + len; ++q){
            if (str[q] == '0')
                startZeros++;
            else
                break;
        }
        // Get end zeros in the current rotation
        for (int q = p + len - 1; q >= p; --q){
            if (str[q] == '0')
                endZeros++;
            else
                break;
        }
        // Get maximum zeros
        maximumZeros = max(startZeros + endZeros, maximumZeros);
    }
    // Return the final answer
    return maximumZeros;
}
int main(){
    // Given string
    string str = "110000010";
    // get string size
    int len = str.size();
    cout << "The maximum sum of start and end zeros in any rotation of binary string is " << maxStartEndZeros(str, len);
    return 0;
}

Output

The maximum sum of start and end zeros in any rotation of binary string is 5

Time complexity – O(N*N), as we find all rotations of the given string.

Space complexity – O(N) as we store the concatenated string.

Approach 2

In this approach, we will find the maximum number of consecutive zeros in the given binary string. Also, we will find the sum of starting and ending zeros in the original string. We will choose between the sum and maximum consecutive zeros according to the maximum value.

Algorithm

Step 1 – Initially, we need to count the total number of zeros in the given string.

Step 2 – If the total number of zeros is equal to the string length, return the string length.

Step 3 – Define the ‘maximumZeros’ and ‘maxConsZeros’ variables to store maximum and maximum consecutive zeros.

Step 4 – While looping through the string, if the current character is ‘0’, increase the value of maxConsZeros by 1.

Step 5 – If the current character is ‘1’, update the maximumZeros variable's value and reinitialize the maxConsZeros variable's value.

Step 6 – Define the left and right variables to store the index of strings.

Step 7 – Traverse the string from the start until we get ‘1’, and increase the value of a left and maxConsZeros variable.

Step 8 - Traverse the string from the end until we get ‘1’, decrease the value of a right, and increase the maxConsZeros variable.

Step 9 – Return the maximumZeros value after updating it.

Example

#include <bits/stdc++.h>
using namespace std;

int maxStartEndZeros(string alpha, int len){
    // To store the total count of zeros in the string
    int count0 = 0;
    // Traverse binary string
    for (int p = 0; p < len; ++p){
        if (alpha[p] == '0')
            count0++;
    }
    // If string contains all zeros, return len
    if (count0 == len){
        return len;
    }
    // to store maximum zeros at start and end
    int maximumZeros = 0;
    // to store maximum consecutive zeros
    int maxConsZeros = 0;
    for (int p = 0; p < len; p++) {
        if (alpha[p] == '0')
            maxConsZeros++;
        else {
            maximumZeros = max(maximumZeros, maxConsZeros);
            // reinitialize maxConsZeros
            maxConsZeros = 0;
        }
    }
    // Get maximum consecutive zeros in the string
    maximumZeros = max(maximumZeros, maxConsZeros);
    // Get sum of consecutive zeros at start and end
    int left = 0, right = len - 1;
    maxConsZeros = 0;
    // Consecutive zeros at the left
    while (alpha[left] != '1' && left < len) {
        maxConsZeros++;
        left++;
    }
    // Consecutive zeros at the right
    while (alpha[right] != '1' && right >= 0){
        maxConsZeros++;
        right--;
    }
    // Get max value
    maximumZeros = max(maximumZeros, maxConsZeros);
    // return final result
    return maximumZeros;
}
int main(){
    // Given string
    string str = "110000010";
    // get string size
    int len = str.size();
    cout << "The maximum sum of start and end zeros in any rotation of binary string is " << maxStartEndZeros(str, len);
    return 0;
}

Output

The maximum sum of start and end zeros in any rotation of binary string is 5

Time complexity – O(N) as we iterate the string.

Space complexity – O(1) as we don’t use any dynamic space.

The second approach is more optimized in terms of time and space complexity as we traverse the string only once without using any extra space. Programmers can also try to find the maximum numbers of ‘1’s at the start and end of the string for more practice with problems like this.

Updated on: 24-Aug-2023

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements