Open In App

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



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:

Below is the implementation of the above approach:




// 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 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




# 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# 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




<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)

 


Article Tags :