Please check the code

what i have done after taking input is

  1. i created an array that store the index of 1’s

  2. then with the help of above array i calculated the max length subarray i got after fliping k zeros

  3. and then stores them in dp nammed vector
    then find max length subarray and return as output

4)then i calculated starting from which index is giving me max length subarray

  1. and traverse the main input array to make changes and display it

but this all is showing some test case wrong pls see

@pankajsingh
An Efficient Solution can solve the problem in O(n) time and O(1) space. 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.

Implementation:

yes ,I know the effective soln . I want to know what’s wrong in my code it’s partially working

@pankajsingh for the input

1 0
0

Expected output:

0
0

Your output:

0

Another one:
input:

7 0
0 1 0 0 0 1 0

expected output

1
0 1 0 0 0 1 0 

your output

2
0 1 0 0 0 1 1 

it’d appear that your code is not working well for cases where k = 0

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.