Help SHENQ

I’m confused about why my brute force solution for the first part is not working!
Basically, I’m taking 4 elements at a time from the back and combining them.
Could not find any counter example to this!

My Code

After four operations array will go to n/4 then you have to check the condition again and again in while loop for possibility of smaller array size optimal answer.

I don’t think I get what you mean by checking the condition. The array will always have elements of the same parity if that is what you were suggesting.

Oh, am sorry I got your solution wrong. Now it seems mostly correct for 50. Cannot find the error.

@doubleux, your code’s output for
1
11
0 0 0 0 0 0 0 0 0 0 0
is as follows:
1
7
and it is the correct answer.
But your output for the following input,
1
12
0 0 0 0 0 0 0 0 0 0 0 0
is as follows:
2
2 5
which is clearly wrong since you can ignore ist zero and reduce the remaining 11 consecutive '0’s into a single ‘7’ (like the previous input of 11 consecutive '0’s)
and the final ans can be
2
0 7
which I believe is the right answer .
(Although both your ans [2,5] and correct answer [0 7] have same length, the correct answers’s first element is less than yours(0<2) and hence it is more beautiful , according to the question)

1 Like

Yeah, You’re right! Got it.