I made a silly mistake, but I am unable to find it. I am not getting correct answer for the second test case. Please help.

p.s. Mistake is in variable k I think

I made a silly mistake, but I am unable to find it. I am not getting correct answer for the second test case. Please help.

p.s. Mistake is in variable k I think

you are pushing the same variable twice on the queue list.

For example, in the second test case:

initially qq = {0};

after loop 1, qq = {1,2};

after loop 2, qq = {2,2}, which is incorrect;

add a variable done[] to avoid this.

This passes the given test cases

Thanks, this now passes the given test cases, but not the problem. Can you tell me where else I have made mistake?