Maximum length of consecutive 1s or 0s after flipping at most K characters
Last Updated :
15 Nov, 2021
Given a binary string S of size N and an integer K, the task is to find the maximum length of consecutive 1s or 0s after flipping at most K characters of the given binary string S.
Examples:
Input: S = “1001”, K = 1
Output: 3
Explanation:
Flip the K(= 1) characters at index 3 of the string modifies the string S to “1000”. Now the maximum length of consecutive 0s is 3, which is the required result.
Input: S = “11011011”, K = 3
Output: 8
Approach: The given problem can be solved using the Two Pointer Approach and Sliding Window approach. Follow the steps to solve the given problem:
- Initialize a function, say maxLength(S, N, ch, K) that find the maximum length of characters ch after flipping at most K characters using the following steps:
- Initialize a variable, say cnt that stores the count of character ch in the window.
- Initialize a variable, say left that stores the starting of the resultant window.
- Initialize a variable, say ans that stores the resultant length of consecutive K characters as ch after at most K flips.
- Traverse the string S using the variable right and perform the following steps:
- If the value of S[right] is ch, then increment the value of cnt by 1.
- Iterate until the value of cnt is greater than K, then increment the left pointer and decrement the value of cnt by 1.
- Update the value of ans to maximum of ans and (right – left + 1).
- After completing the above steps, print the maximum of the value returned by the maxLength(S, N, ‘0’, K) and maxLength(S, N, ‘1’, K) as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxLength(string str, int n,
char c, int k)
{
int ans = -1;
int cnt = 0;
int left = 0;
for ( int right = 0; right < n; right++) {
if (str[right] == c) {
cnt++;
}
while (cnt > k) {
if (str[left] == c) {
cnt--;
}
left++;
}
ans = max(ans, right - left + 1);
}
return ans;
}
int maxConsecutiveSegment(string S, int K)
{
int N = S.length();
return max(maxLength(S, N, '0' , K),
maxLength(S, N, '1' , K));
}
int main()
{
string S = "1001" ;
int K = 1;
cout << maxConsecutiveSegment(S, K);
return 0;
}
|
Java
public class GFG {
static int maxLength(String str, int n,
char c, int k)
{
int ans = - 1 ;
int cnt = 0 ;
int left = 0 ;
for ( int right = 0 ; right < n; right++) {
if (str.charAt(right) == c) {
cnt++;
}
while (cnt > k) {
if (str.charAt(left) == c) {
cnt--;
}
left++;
}
ans = Math.max(ans, right - left + 1 );
}
return ans;
}
static int maxConsecutiveSegment(String S, int K)
{
int N = S.length();
return Math.max(maxLength(S, N, '0' , K),
maxLength(S, N, '1' , K));
}
int main()
{
return 0 ;
}
public static void main (String[] args) {
String S = "1001" ;
int K = 1 ;
System.out.println(maxConsecutiveSegment(S, K));
}
}
|
Python3
def maxLength( str , n, c, k):
ans = - 1
cnt = 0
left = 0
for right in range ( 0 , n):
if ( str [right] = = c):
cnt + = 1
while (cnt > k):
if ( str [left] = = c):
cnt - = 1
left + = 1
ans = max (ans, right - left + 1 )
return ans
def maxConsecutiveSegment(S, K):
N = len (S)
return max (maxLength(S, N, '0' , K), maxLength(S, N, '1' , K))
if __name__ = = "__main__" :
S = "1001"
K = 1
print (maxConsecutiveSegment(S, K))
|
C#
using System;
public class GFG
{
static int maxLength(String str, int n,
char c, int k)
{
int ans = -1;
int cnt = 0;
int left = 0;
for ( int right = 0; right < n; right++)
{
if (str[right] == c)
{
cnt++;
}
while (cnt > k)
{
if (str[left] == c)
{
cnt--;
}
left++;
}
ans = Math.Max(ans, right - left + 1);
}
return ans;
}
static int maxConsecutiveSegment(String S, int K)
{
int N = S.Length;
return Math.Max(maxLength(S, N, '0' , K),
maxLength(S, N, '1' , K));
}
public static void Main()
{
String S = "1001" ;
int K = 1;
Console.WriteLine(maxConsecutiveSegment(S, K));
}
}
|
Javascript
<script>
const maxLength = (str, n, c, k) => {
let ans = -1;
let cnt = 0;
let left = 0;
for (let right = 0; right < n; right++) {
if (str[right] == c) {
cnt++;
}
while (cnt > k) {
if (str[left] == c) {
cnt--;
}
left++;
}
ans = Math.max(ans, right - left + 1);
}
return ans;
}
const maxConsecutiveSegment = (S, K) => {
let N = S.length;
return Math.max(maxLength(S, N, '0' , K), maxLength(S, N, '1' , K));
}
let S = "1001" ;
let K = 1;
document.write(maxConsecutiveSegment(S, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...