1 count Dynamic Programming

I am getting the correct length of sub array, but as told in the video we need to iterate till right iterator reaches null, the array that i am getting is different from what the sample output should be.
Sample Output is :
5
1 0 0 1 1 1 1 1 0 1

and what i am getting is :
5
1 0 0 1 0 1 1 1 1 1

Here is the code: https://ide.codingblocks.com/s/107203

Hello @Kishan-Mishra-2464674013761441,

If you observe these outputs very carefully, the length is same but 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.
Give a like, if you are satisfied.

I know that i am solving even after getting the answer at first time (by traversing till the end of the array), but then how will i stop once i get the correct length and array, and what if i get length as 5 and zero count reaches equal to ‘k’ and i stop iterating further saying it is the final answer, but if iterated further the length becomes 6 which would be maximum and will be the correct answer.

I didn’t suggest to stop iterating.

I maodified your code:

Read the comments for better understanding.

Hope, this would help.
Give a like, if satisfied.

2 Likes

Thank you. I got it now.

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.

Consider this solution

why you use length as r-l why it is not r-l+1