Getting the Integer value right but unable to print the array correctly

I used the 2 pointer approach and am getting the right value for the Longest Sequence of one, but the storage array being printed in wrong.

difference is in the 0 which have been replaced.
What is happening in your case,
you are updating the array, even if the max count is same as previously obtained.

Solution:
If there are multiple possibilities of substrings with max count or length, then only update the first possibility your code encounters.
In simple words, do not update for equality.

Hope, this would help.

I am even getting TLE , could you please recommend a better approach ?

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.

I still am unable to get the correct answer, could you maybe share the solution for the same

see this:


if this solves your doubt please mark it as resolved