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[2] 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[2] 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++
#include <bits/stdc++.h>
using namespace std;
int maxGroupOfZeroes(string str, int X)
{
int n = str.size();
int groups = 0;
int i = 0;
while (i < n) {
int zero = 0;
while (i < n && str[i] == '0' ) {
zero++;
i++;
}
i++;
groups += (zero / X);
}
int left = 0;
int right = 0;
i = 0;
while (i < n) {
char ch = str[i];
if (ch == '0' ) {
left++;
i++;
}
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));
if (without != contribute) {
return ++groups;
}
left = right;
right = 0;
}
}
return groups;
}
int main()
{
string str = "0010001" ;
int X = 2;
int max_groups = maxGroupOfZeroes(str, X);
cout << (max_groups);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int maxGroupOfZeroes(
String str, int X)
{
int n = str.length();
int groups = 0 ;
int i = 0 ;
while (i < n) {
int zero = 0 ;
while (i < n
&& str.charAt(i) == '0' ) {
zero++;
i++;
}
i++;
groups += (zero / X);
}
int left = 0 ;
int right = 0 ;
i = 0 ;
while (i < n) {
char ch = str.charAt(i);
if (ch == '0' ) {
left++;
i++;
}
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));
if (without != contribute) {
return ++groups;
}
left = right;
right = 0 ;
}
}
return groups;
}
public static void main(String[] args)
{
String str = "0010001" ;
int X = 2 ;
int max_groups
= maxGroupOfZeroes(str, X);
System.out.println(max_groups);
}
}
|
Python
def maxGroupOfZeroes( str , X):
n = len ( str )
groups = 0
i = 0
while (i < n):
zero = 0
while (i < n and str [i] = = '0' ):
zero + = 1
i + = 1
i + = 1
groups + = (zero / / X)
left = 0
right = 0
j = 0
while (j < n):
ch = str [j]
if (ch = = '0' ):
left + = 1
j + = 1
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))
if (without ! = contribute):
return 1 + groups
left = right
right = 0
return groups
str = "0010001"
X = 2
max_groups = maxGroupOfZeroes( str , X)
print (max_groups)
|
C#
using System;
class GFG {
public static int maxGroupOfZeroes(
String str, int X)
{
int n = str.Length;
int groups = 0;
int i = 0;
while (i < n) {
int zero = 0;
while (i < n
&& str[i] == '0' ) {
zero++;
i++;
}
i++;
groups += (zero / X);
}
int left = 0;
int right = 0;
i = 0;
while (i < n) {
char ch = str[i];
if (ch == '0' ) {
left++;
i++;
}
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));
if (without != contribute) {
return ++groups;
}
left = right;
right = 0;
}
}
return groups;
}
public static void Main()
{
String str = "0010001" ;
int X = 2;
int max_groups
= maxGroupOfZeroes(str, X);
Console.Write(max_groups);
}
}
|
Javascript
<script>
function maxGroupOfZeroes(
str, X) {
let n = str.length;
let groups = 0;
let i = 0;
while (i < n) {
let zero = 0;
while (i < n
&& str[i] == '0' ) {
zero++;
i++;
}
i++;
groups += Math.floor((zero / X));
}
let left = 0;
let right = 0;
i = 0;
while (i < n) {
let ch = str[i];
if (ch == '0' ) {
left++;
i++;
}
else {
let j = i;
i++;
while (i < n
&& str[i] != '1' ) {
i++;
right++;
}
let contribute
= Math.floor((left + right + 1) / X);
let without
= (Math.floor((left / X) + (right / X)));
if (without != contribute) {
return ++groups;
}
left = right;
right = 0;
}
}
return groups;
}
let str = "0010001" ;
let X = 2;
let max_groups
= maxGroupOfZeroes(str, X);
document.write(max_groups);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...