#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;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBGdW5jdGlvbiB0byBmaW5kIHRoZSBtYXhpbXVtIGxlbmd0aAovLyBjb250aW51b3VzIHNlZ21lbnQgb2YgY2hhcmFjdGVyIGMgYWZ0ZXIKLy8gZmxpcHBpbmcgYXQgbW9zdCBLIGNoYXJhY3RlcnMKaW50IG1heExlbmd0aChzdHJpbmcgc3RyLCBpbnQgbiwKCQkJY2hhciBjLCBpbnQgaykKewoJLy8gU3RvcmVzIHRoZSBtYXhpbXVtIGxlbmd0aAoJaW50IGFucyA9IC0xOwoKCS8vIFN0b3JlcyB0aGUgY291bnQgb2YgY2hhciAnYycKCWludCBjbnQgPSAwOwoKCS8vIFN0YXJ0IG9mIHdpbmRvdwoJaW50IGxlZnQgPSAwOwoKCWZvciAoaW50IHJpZ2h0ID0gMDsgcmlnaHQgPCBuOyByaWdodCsrKSB7CgoJCWlmIChzdHJbcmlnaHRdID09IGMpIHsKCQkJY250Kys7CgkJfQoKCQkvLyBSZW1vdmUgdGhlIGV4dHJh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