#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll n,k;
cin>>n>>k;
ll arr[n];
for(ll i=0;i<n;i++)
{
cin>>arr[i];
}
ll left_count=0;
ll right_count=0;
ll zero_count=0;
ll max_subarray=0;
ll max_right_count=0;
ll max_left_count=0;
while(zero_count<=k && right_count<=n)
{
if(arr[right_count]==0)
{
zero_count++;
}
while(zero_count>k)
{
if(arr[left_count]==0)
{
zero_count–;
}
left_count++;
}
if(right_count-left_count>max_subarray)
{
max_right_count=right_count;
max_left_count=left_count;
max_subarray=right_count-left_count+1;
}
right_count++;
}
cout<<max_right_count-max_left_count+1<<endl;
for(ll i=0;i<n;i++)
{
if(i>=max_left_count && i<=max_right_count)
{
cout<<1<<" “;
}
else
cout<<arr[i]<<” ";
}
return 0;
}
All test cases not getting accepted problem: 1 count
Instead of using rightcount- leftcount > max
Do rightcount- leftcount+1> max. It will work.
I think you missed this condition
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.