Why giving output withhot taking input

#include
using namespace std;
int main(){
int n,k;
cin>>n>>k;
int a[n];
for(int j=0;j<n;j++){
cin>>a[j];
}
int i=0;
int j=0;
int p,q;
int max=0;
int flag=0;
while(k>0){

flag=1;

if(a[j]==0){

k--;

}
j++;
}
if(max<j-i){
p=i;
q=j;
max=j-i;
}

while(j<n){
if(a[j]==0){
while(a[i]!=0){
i++;
}
}
else{
j++;
}
if(max>j-i){
p=i;
q=j;
max=j-i;
}
}
if(max>j-i){
p=i;
q=j;
max=j-i;
}
cout<<max+1<<"\n";

for(int i=p;i<=q;i++){
a[i]=1;
}
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}

}

Save your code on ide.codingblocks.com and then share its link.

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.

Sir,without giving reply how you cleared my doubt.please reply sir

@dineshjani
Please save your code on ide.codingblocks.com and then share its link.

The code you have provided is not running and throwing errors.
Dry run and check once.

Also verify the approach:
The idea is to use Sliding Window for the given array. The solution is taken from here. Let us use a window covering from index wL to index wR. Let the number of zeros inside the window be zeroCount. We maintain the window with at most m zeros inside.

The main steps are:

While zeroCount is no more than m: expand the window to the right (wR++) and update the count zeroCount.
While zeroCount exceeds m, shrink the window from left (wL++), update zeroCount;
Update the widest window along the way. The positions of output zeros are inside the best window.

You can refer this https://ide.codingblocks.com/s/284505

Also if you want me to check your code please add some comments to it.So that i can know how you are approaching