# Maximum length of consecutive 1s or 0s after flipping at most K characters

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**:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

Input:S = “1001”, K = 1Output:3Explanation:

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 = 3Output: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)**.

- If the value of

- Initialize a variable, say
- 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++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the maximum length` `// continuous segment of character c after` `// flipping at most K characters` `int` `maxLength(string str, ` `int` `n,` ` ` `char` `c, ` `int` `k)` `{` ` ` `// Stores the maximum length` ` ` `int` `ans = -1;` ` ` `// Stores the count of char 'c'` ` ` `int` `cnt = 0;` ` ` `// Start of window` ` ` `int` `left = 0;` ` ` `for` `(` `int` `right = 0; right < n; right++) {` ` ` `if` `(str[right] == c) {` ` ` `cnt++;` ` ` `}` ` ` `// Remove the extra 'c' from left` ` ` `while` `(cnt > k) {` ` ` `if` `(str[left] == c) {` ` ` `cnt--;` ` ` `}` ` ` `// Increment the value of` ` ` `// the left` ` ` `left++;` ` ` `}` ` ` `// Update the resultant maximum` ` ` `// length of character ch` ` ` `ans = max(ans, right - left + 1);` ` ` `}` ` ` `return` `ans;` `}` `// Function to find the maximum length` `// of consecutive 0s or 1s by flipping` `// at most K characters of the string` `int` `maxConsecutiveSegment(string S, ` `int` `K)` `{` ` ` `int` `N = S.length();` ` ` `// Print the maximum of the maximum` ` ` `// length of 0s or 1s` ` ` `return` `max(maxLength(S, N, ` `'0'` `, K),` ` ` `maxLength(S, N, ` `'1'` `, K));` `}` `// Driver Code` `int` `main()` `{` ` ` `string S = ` `"1001"` `;` ` ` `int` `K = 1;` ` ` `cout << maxConsecutiveSegment(S, K);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `public` `class` `GFG {` ` ` ` ` `// Function to find the maximum length` ` ` `// continuous segment of character c after` ` ` `// flipping at most K characters` ` ` `static` `int` `maxLength(String str, ` `int` `n,` ` ` `char` `c, ` `int` `k)` ` ` `{` ` ` ` ` `// Stores the maximum length` ` ` `int` `ans = -` `1` `;` ` ` ` ` `// Stores the count of char 'c'` ` ` `int` `cnt = ` `0` `;` ` ` ` ` `// Start of window` ` ` `int` `left = ` `0` `;` ` ` ` ` `for` `(` `int` `right = ` `0` `; right < n; right++) {` ` ` ` ` `if` `(str.charAt(right) == c) {` ` ` `cnt++;` ` ` `}` ` ` ` ` `// Remove the extra 'c' from left` ` ` `while` `(cnt > k) {` ` ` `if` `(str.charAt(left) == c) {` ` ` `cnt--;` ` ` `}` ` ` ` ` `// Increment the value of` ` ` `// the left` ` ` `left++;` ` ` `}` ` ` ` ` `// Update the resultant maximum` ` ` `// length of character ch` ` ` `ans = Math.max(ans, right - left + ` `1` `);` ` ` `}` ` ` ` ` `return` `ans;` ` ` `}` ` ` ` ` `// Function to find the maximum length` ` ` `// of consecutive 0s or 1s by flipping` ` ` `// at most K characters of the string` ` ` `static` `int` `maxConsecutiveSegment(String S, ` `int` `K)` ` ` `{` ` ` `int` `N = S.length();` ` ` ` ` `// Print the maximum of the maximum` ` ` `// length of 0s or 1s` ` ` `return` `Math.max(maxLength(S, N, ` `'0'` `, K),` ` ` `maxLength(S, N, ` `'1'` `, K));` ` ` `}` ` ` ` ` `// Driver Code` ` ` `int` `main()` ` ` `{` ` ` ` ` `return` `0` `;` ` ` `}` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `main (String[] args) {` ` ` ` ` `String S = ` `"1001"` `;` ` ` `int` `K = ` `1` `;` ` ` `System.out.println(maxConsecutiveSegment(S, K));` ` ` ` ` `}` `}` `// This code is contributed by AnkThon` |

## Python3

`# python program for the above approach` `# Function to find the maximum length` `# continuous segment of character c after` `# flipping at most K characters` `def` `maxLength(` `str` `, n, c, k):` ` ` `# Stores the maximum length` ` ` `ans ` `=` `-` `1` ` ` `# Stores the count of char 'c'` ` ` `cnt ` `=` `0` ` ` `# Start of window` ` ` `left ` `=` `0` ` ` `for` `right ` `in` `range` `(` `0` `, n):` ` ` `if` `(` `str` `[right] ` `=` `=` `c):` ` ` `cnt ` `+` `=` `1` ` ` `# Remove the extra 'c' from left` ` ` `while` `(cnt > k):` ` ` `if` `(` `str` `[left] ` `=` `=` `c):` ` ` `cnt ` `-` `=` `1` ` ` `# Increment the value of` ` ` `# the left` ` ` `left ` `+` `=` `1` ` ` `# Update the resultant maximum` ` ` `# length of character ch` ` ` `ans ` `=` `max` `(ans, right ` `-` `left ` `+` `1` `)` ` ` `return` `ans` `# Function to find the maximum length` `# of consecutive 0s or 1s by flipping` `# at most K characters of the string` `def` `maxConsecutiveSegment(S, K):` ` ` `N ` `=` `len` `(S)` ` ` `# Print the maximum of the maximum` ` ` `# length of 0s or 1s` ` ` `return` `max` `(maxLength(S, N, ` `'0'` `, K), maxLength(S, N, ` `'1'` `, K))` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `S ` `=` `"1001"` ` ` `K ` `=` `1` ` ` `print` `(maxConsecutiveSegment(S, K))` `# This code is contributed by rakeshsahni` |

## C#

`// C# program for the above approach` `using` `System;` `public` `class` `GFG` `{` ` ` `// Function to find the maximum length` ` ` `// continuous segment of character c after` ` ` `// flipping at most K characters` ` ` `static` `int` `maxLength(String str, ` `int` `n,` ` ` `char` `c, ` `int` `k)` ` ` `{` ` ` `// Stores the maximum length` ` ` `int` `ans = -1;` ` ` `// Stores the count of char 'c'` ` ` `int` `cnt = 0;` ` ` `// Start of window` ` ` `int` `left = 0;` ` ` `for` `(` `int` `right = 0; right < n; right++)` ` ` `{` ` ` `if` `(str[right] == c)` ` ` `{` ` ` `cnt++;` ` ` `}` ` ` `// Remove the extra 'c' from left` ` ` `while` `(cnt > k)` ` ` `{` ` ` `if` `(str[left] == c)` ` ` `{` ` ` `cnt--;` ` ` `}` ` ` `// Increment the value of` ` ` `// the left` ` ` `left++;` ` ` `}` ` ` `// Update the resultant maximum` ` ` `// length of character ch` ` ` `ans = Math.Max(ans, right - left + 1);` ` ` `}` ` ` `return` `ans;` ` ` `}` ` ` `// Function to find the maximum length` ` ` `// of consecutive 0s or 1s by flipping` ` ` `// at most K characters of the string` ` ` `static` `int` `maxConsecutiveSegment(String S, ` `int` `K)` ` ` `{` ` ` `int` `N = S.Length;` ` ` `// Print the maximum of the maximum` ` ` `// length of 0s or 1s` ` ` `return` `Math.Max(maxLength(S, N, ` `'0'` `, K),` ` ` `maxLength(S, N, ` `'1'` `, K));` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `String S = ` `"1001"` `;` ` ` `int` `K = 1;` ` ` `Console.WriteLine(maxConsecutiveSegment(S, K));` ` ` `}` `}` `// This code is contributed by Saurabh Jaiswal` |

## Javascript

`<script>` ` ` `// JavaScript program for the above approach` ` ` `// Function to find the maximum length` ` ` `// continuous segment of character c after` ` ` `// flipping at most K characters` ` ` `const maxLength = (str, n, c, k) => {` ` ` `// Stores the maximum length` ` ` `let ans = -1;` ` ` `// Stores the count of char 'c'` ` ` `let cnt = 0;` ` ` `// Start of window` ` ` `let left = 0;` ` ` `for` `(let right = 0; right < n; right++) {` ` ` `if` `(str[right] == c) {` ` ` `cnt++;` ` ` `}` ` ` `// Remove the extra 'c' from left` ` ` `while` `(cnt > k) {` ` ` `if` `(str[left] == c) {` ` ` `cnt--;` ` ` `}` ` ` `// Increment the value of` ` ` `// the left` ` ` `left++;` ` ` `}` ` ` `// Update the resultant maximum` ` ` `// length of character ch` ` ` `ans = Math.max(ans, right - left + 1);` ` ` `}` ` ` `return` `ans;` ` ` `}` ` ` `// Function to find the maximum length` ` ` `// of consecutive 0s or 1s by flipping` ` ` `// at most K characters of the string` ` ` `const maxConsecutiveSegment = (S, K) => {` ` ` `let N = S.length;` ` ` `// Print the maximum of the maximum` ` ` `// length of 0s or 1s` ` ` `return` `Math.max(maxLength(S, N, ` `'0'` `, K), maxLength(S, N, ` `'1'` `, K));` ` ` `}` ` ` `// Driver Code` ` ` `let S = ` `"1001"` `;` ` ` `let K = 1;` ` ` `document.write(maxConsecutiveSegment(S, K));` `// This code is contributed by rakeshsahni` `</script>` |

**Output:**

3

**Time Complexity:** O(N)**Auxiliary Space:** O(1)