You are given a binary string s of size n containing only 0s and 1s. A substring of size k is called good if the count of all 1s in this substring is not greater than m.
You are required to transform a string in such a manner that every substring of size k are good by performing some operations. In one operation, you can change the value of a character in a string from 1 to 0.
Determine the minimum number of operations that are required such that every substring of size k in the given string is good.

Input 


12 4 2
111000111011

Output


2
110000110011

   Solution


  #include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n,k,m;
cin>>n>>k>>m;
string s;
cin>>s;
int ans=0;
int j=0;
int c1=0;
for(int i=0;i<k;i++)
{
if(s[i]=='1')
{
c1++;
if(c1>m)
{
c1--;
s[i]='0';
ans++;
}
}
}
char c=s[j++];
for(int i=k;i<n;i++)
{
if(c=='1')
c1--;
if(s[i]=='1')
{
c1++;
if(c1>m)
{
s[i]='0';
c1--;
ans++;
}
}
c=s[j++];
}
cout<<ans<<"\n";
cout<<s;
}


if you have any problem in this code, take a screenshot and contact with Rishabh Chaubey.

Post a Comment

Previous Post Next Post