FLIP - Editorial

Can you explain how the outcome is 3.
As I think answer should be 2 bcz in one step I’ll select the whole substring of length 13 and make required changes, then in the next step I would select substring of length 11 from index 1 to 11(zero based indexing) and make required changes. Thus the answer should be 2.

Also I think there exist no case when the output will be greater than 2.

Correct me please if I’m going wrong !!

1 Like

While selecting whole string of length 13 , all elements at odd positions are flipped.
Considering 1-based indexing

index of elements which will be flipped-1,3,5,7,9,11,13
index of elements which we wanted to be flipped -1,3,5,7,13

So , elements at index 9 and 11 are also flipped,
which we don’t want to be flipped.

Considering 1-based indexing

if you choose substring from index 2 to index 12:

index of elements which will be flipped-2,4,6,8,10,12
index of elements which we wanted to be flipped -2,4,6

So , elements at index 8,10,12 are also flipped,
which we don’t want to be flipped.

I hope you got the answer.

Understood now?

Got it … The key word was we need to change * all * the bits and not the ones which we wanted to do.
Thanks man for such a nice explanation !!

1 Like

Yep same approach but my code sucks … good explanation :+1: