 Open in App
Not now

# Maximize subarrays of 0s of length X in given Binary String after flipping at most one ‘1’

• Last Updated : 12 Jul, 2022

Given a binary string str of length N and a positive integer X, the task is to maximize the count of X length subarrays consisting of only 0‘s by flipping at most one 1. One bit will be considered in only one subarray.
Example:

Input: str =0010001″, X = 2
Output: 3
Explanation: Flip str from 1 to 0, -> str = “0000001” -> Now count of X length subarrays of 0 = 3
Flipping index 6 will also give resultant count as 3.

Input: str = “00100”, X = 2
Output: 2
Explanation: Flipping str from 1 to 0 will not change the maximum group of 0’s.It will remain same as 2 only.

Approach: To calculate maximum group of zeroes containing X zeroes follow the below given steps:

• Find number of groups of zeroes containing X continuous zeroes.
• Iterate over the given string and at the same time maintain the count of left zeroes and right zeroes if ‘1’ appears during iterating.
• Now check if we flip current ‘1’ will it affect the group of zeroes or no, if it will then increment the group by 1 and return it, else continue the iteration and do the above steps again and again.
• Return number of groups.

Below is the implementation of the above approach:

## C++

 `// C++ program of finding` `// the maximum group of zeroes` `// in a binary string after` `// flipping at most one '1'`   `#include ` `using` `namespace` `std;`   `// Function to find the maximum` `// number of groups of X` `// consecutive Zeroes` `int` `maxGroupOfZeroes(string str, ``int` `X)` `{` `  ``int` `n = str.size();`   `  ``// Number of groups before flipping '1'` `  ``int` `groups = 0;`   `  ``// Iterating over the string` `  ``int` `i = 0;` `  ``while` `(i < n) {`   `    ``// It will keep the track` `    ``// of consecutive zeroes` `    ``int` `zero = 0;` `    ``while` `(i < n && str[i] == ``'0'``) {` `      ``zero++;` `      ``i++;` `    ``}` `    ``i++;`   `    ``// It will break those` `    ``// consecutive zeroes into groups` `    ``groups += (zero / X);` `  ``}`   `  ``// Left variable will count` `  ``// the consecutive zeroes` `  ``// on the left side of '1'` `  ``int` `left = 0;`   `  ``// Right variable will count` `  ``// the consecutive zeroes` `  ``// on the right size of '1'` `  ``int` `right = 0;`   `  ``// Iterating str from 0 index` `  ``i = 0;` `  ``while` `(i < n) {`   `    ``char` `ch = str[i];`   `    ``// If it is '0' keep` `    ``// incrementing left zeroes` `    ``if` `(ch == ``'0'``) {` `      ``left++;` `      ``i++;` `    ``}` `    ``// Else increment` `    ``// right zeroes while` `    ``// character is '0'` `    ``else` `{`   `      ``int` `j = i;` `      ``i++;` `      ``while` `(i < n && str[i] != ``'1'``) {` `        ``i++;` `        ``right++;` `      ``}`   `      ``int` `contribute = (left + right + 1) / X;` `      ``int` `without = ((left / X) + (right / X));`   `      ``// Checking after flipping` `      ``// current '1' will` `      ``// it affect the groups or not` `      ``if` `(without != contribute) {` `        ``return` `++groups;` `      ``}`   `      ``// If it doesn't affect` `      ``// then right zeroes` `      ``// will become left zeroes` `      ``// and for right` `      ``// zeroes we will have` `      ``// to count it again` `      ``left = right;` `      ``right = 0;` `    ``}` `  ``}`   `  ``// Return number of groups` `  ``return` `groups;` `}`   `// Driver Code` `int` `main()` `{` `  ``string str = ``"0010001"``;` `  ``int` `X = 2;` `  ``int` `max_groups = maxGroupOfZeroes(str, X);` `  ``cout << (max_groups);`   `  ``return` `0;` `}`   `// This code is contributed by rakeshsahni`

## Java

 `// Java program of finding` `// the maximum group of zeroes` `// in a binary string after` `// flipping at most one '1'`   `import` `java.util.*;`   `class` `GFG {`   `    ``// Function to find the maximum` `    ``// number of groups of X` `    ``// consecutive Zeroes` `    ``public` `static` `int` `maxGroupOfZeroes(` `        ``String str, ``int` `X)` `    ``{` `        ``int` `n = str.length();`   `        ``// Number of groups before flipping '1'` `        ``int` `groups = ``0``;`   `        ``// Iterating over the string` `        ``int` `i = ``0``;` `        ``while` `(i < n) {`   `            ``// It will keep the track` `            ``// of consecutive zeroes` `            ``int` `zero = ``0``;` `            ``while` `(i < n` `                   ``&& str.charAt(i) == ``'0'``) {` `                ``zero++;` `                ``i++;` `            ``}` `            ``i++;`   `            ``// It will break those` `            ``// consecutive zeroes into groups` `            ``groups += (zero / X);` `        ``}`   `        ``// Left variable will count` `        ``// the consecutive zeroes` `        ``// on the left side of '1'` `        ``int` `left = ``0``;`   `        ``// Right variable will count` `        ``// the consecutive zeroes` `        ``// on the right size of '1'` `        ``int` `right = ``0``;`   `        ``// Iterating str from 0 index` `        ``i = ``0``;` `        ``while` `(i < n) {`   `            ``char` `ch = str.charAt(i);`   `            ``// If it is '0' keep` `            ``// incrementing left zeroes` `            ``if` `(ch == ``'0'``) {` `                ``left++;` `                ``i++;` `            ``}` `            ``// Else increment` `            ``// right zeroes while` `            ``// character is '0'` `            ``else` `{`   `                ``int` `j = i;` `                ``i++;` `                ``while` `(i < n` `                       ``&& str.charAt(i) != ``'1'``) {` `                    ``i++;` `                    ``right++;` `                ``}`   `                ``int` `contribute` `                    ``= (left + right + ``1``) / X;` `                ``int` `without` `                    ``= ((left / X) + (right / X));`   `                ``// Checking after flipping` `                ``// current '1' will` `                ``// it affect the groups or not` `                ``if` `(without != contribute) {` `                    ``return` `++groups;` `                ``}`   `                ``// If it doesn't affect` `                ``// then right zeroes` `                ``// will become left zeroes` `                ``// and for right` `                ``// zeroes we will have` `                ``// to count it again` `                ``left = right;` `                ``right = ``0``;` `            ``}` `        ``}`   `        ``// Return number of groups` `        ``return` `groups;` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``String str = ``"0010001"``;` `        ``int` `X = ``2``;` `        ``int` `max_groups` `            ``= maxGroupOfZeroes(str, X);` `        ``System.out.println(max_groups);` `    ``}` `}`

## Python

 `# Python program of finding` `# the maximum group of zeroes` `# in a binary string after` `# flipping at most one '1'`   `# Function to find the maximum` `# number of groups of X` `# consecutive Zeroes` `def` `maxGroupOfZeroes(``str``, X):`   `    ``n ``=` `len``(``str``)`   `    ``# Number of groups before flipping '1'` `    ``groups ``=` `0`   `    ``# Iterating over the string` `    ``i ``=` `0` `    ``while` `(i < n):`   `        ``# It will keep the track` `        ``# of consecutive zeroes` `        ``zero ``=` `0` `        ``while` `(i < n ``and` `str``[i] ``=``=` `'0'``):` `            ``zero ``+``=` `1` `            ``i ``+``=` `1`   `        ``i ``+``=` `1`   `        ``# It will break those` `        ``# consecutive zeroes into groups` `        ``groups ``+``=` `(zero ``/``/` `X)`   `    ``# Left variable will count` `    ``# the consecutive zeroes` `    ``#  on the left side of '1'` `    ``left ``=` `0`   `    ``# Right variable will count` `    ``# the consecutive zeroes` `    ``# on the right size of '1'` `    ``right ``=` `0`   `    ``# Iterating str from 0 index` `    ``j ``=` `0` `    ``while` `(j < n):`   `        ``ch ``=` `str``[j]`   `        ``# If it is '0' keep` `        ``# incrementing left zeroes` `        ``if` `(ch ``=``=` `'0'``):` `            ``left ``+``=` `1` `            ``j ``+``=` `1`   `        ``# Else increment` `        ``# right zeroes while` `        ``# character is '0'` `        ``else``:`   `            ``k ``=` `j` `            ``j ``+``=` `1` `            ``while` `(j < n ``and` `str``[j] !``=` `'1'``):` `                ``j ``+``=` `1` `                ``right ``+``=` `1`   `            ``contribute ``=` `(left ``+` `right ``+` `1``) ``/``/` `X` `            ``without ``=` `((left ``/``/` `X) ``+` `(right ``/``/` `X))`   `            ``# Checking after flipping` `            ``# current '1' will` `            ``# it affect the groups or not` `            ``if` `(without !``=` `contribute):` `                ``return` `1` `+` `groups`   `            ``#  If it doesn't affect` `            ``#  then right zeroes` `            ``# will become left zeroes` `            ``# and for right` `            ``# zeroes we will have` `            ``# to count it again` `            ``left ``=` `right` `            ``right ``=` `0`   `    ``# Return number of groups` `    ``return` `groups`   `# Driver Code` `str` `=` `"0010001"` `X ``=` `2` `max_groups ``=` `maxGroupOfZeroes(``str``, X)` `print``(max_groups)`   `# This code is contributed by Samim Hossain Mondal.`

## C#

 `// C# program of finding` `// the maximum group of zeroes` `// in a binary string after` `// flipping at most one '1'` `using` `System;` `class` `GFG {`   `  ``// Function to find the maximum` `  ``// number of groups of X` `  ``// consecutive Zeroes` `  ``public` `static` `int` `maxGroupOfZeroes(` `    ``String str, ``int` `X)` `  ``{` `    ``int` `n = str.Length;`   `    ``// Number of groups before flipping '1'` `    ``int` `groups = 0;`   `    ``// Iterating over the string` `    ``int` `i = 0;` `    ``while` `(i < n) {`   `      ``// It will keep the track` `      ``// of consecutive zeroes` `      ``int` `zero = 0;` `      ``while` `(i < n` `             ``&& str[i] == ``'0'``) {` `        ``zero++;` `        ``i++;` `      ``}` `      ``i++;`   `      ``// It will break those` `      ``// consecutive zeroes into groups` `      ``groups += (zero / X);` `    ``}`   `    ``// Left variable will count` `    ``// the consecutive zeroes` `    ``// on the left side of '1'` `    ``int` `left = 0;`   `    ``// Right variable will count` `    ``// the consecutive zeroes` `    ``// on the right size of '1'` `    ``int` `right = 0;`   `    ``// Iterating str from 0 index` `    ``i = 0;` `    ``while` `(i < n) {`   `      ``char` `ch = str[i];`   `      ``// If it is '0' keep` `      ``// incrementing left zeroes` `      ``if` `(ch == ``'0'``) {` `        ``left++;` `        ``i++;` `      ``}` `      ``// Else increment` `      ``// right zeroes while` `      ``// character is '0'` `      ``else` `{`   `        ``int` `j = i;` `        ``i++;` `        ``while` `(i < n` `               ``&& str[i] != ``'1'``) {` `          ``i++;` `          ``right++;` `        ``}`   `        ``int` `contribute` `          ``= (left + right + 1) / X;` `        ``int` `without` `          ``= ((left / X) + (right / X));`   `        ``// Checking after flipping` `        ``// current '1' will` `        ``// it affect the groups or not` `        ``if` `(without != contribute) {` `          ``return` `++groups;` `        ``}`   `        ``// If it doesn't affect` `        ``// then right zeroes` `        ``// will become left zeroes` `        ``// and for right` `        ``// zeroes we will have` `        ``// to count it again` `        ``left = right;` `        ``right = 0;` `      ``}` `    ``}`   `    ``// Return number of groups` `    ``return` `groups;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main()` `  ``{` `    ``String str = ``"0010001"``;` `    ``int` `X = 2;` `    ``int` `max_groups` `      ``= maxGroupOfZeroes(str, X);` `    ``Console.Write(max_groups);` `  ``}` `}`   `// This code is contributed by saurabh_jaiswal.`

## Javascript

 ``

Output

`3`

Time Complexity: O(N)
Auxiliary Space: O(1), since no extra space has been taken.

My Personal Notes arrow_drop_up
Related Articles