1 Count - Dynamic Programmimg

Hello sir/ma’am, my code passed all the test cases given in the question. But there’s one test case i found out that my code isn’t passing, n=10, k=2, arr= 1 0 0 0 0 1 0 1 0 1. Here maxLen should be 5 and final array= 1 0 0 0 0 1 1 1 1 1. But according to my code output is coming as maxLen= 4,
1 0 0 0 1 1 1 1 0 1 . So please tell me what changes do i need to make in my code.

Hi @priyanshi.agarwal3405

Please send the cb ide link of your code.

I have made some changes. Now it works.

Link : https://ide.codingblocks.com/s/211049

Hope it Helps.

Sir, I looked at your code. It works fine. Thank you!! But still I have a doubt that basically you removed the while loop i used in else case (line no 22 in my code). So how was that while loop causing a problem to the desired output in the test case i mentioned earlier??

It was an overkill. Hence i used a simple approach. But if you need to keep it, then you just need to place the maxlen checking if statement at the end of the while loop checking r<n. Link : https://ide.codingblocks.com/s/211928

Hope it Helps.

Okay sir… Thank you so much!!!