For 1, 1, 0, 0, 1, 1, 0, 0, 1 what should be expected ? 2 or 4?


Here i think only two switches are sufficient.
if 2 and 6 should be turned on. therefore 3 and 7 will also light up therefore all will be ON


If you start from the first element, the traversal would be like this.

1st switch is already on. Move ahead.
2nd switch is already on. Move ahead.

Now you turn **3rd Switch on. ** total_changes: 1

It will change the states of elements on the right of 3rd.

So states will now become 1,1,1,1,0,0,1,1,0

4th Switch is on.Move ahead.
Make 5th Switch on. total_changes: 2
Changes element to right of 5th : 1,1,1,1,1,1,0,0,1

6th Switch is on. Move ahead.
Make 7th Switch on. total_changes:3
Changes element on right of 7th : 1,1,1,1,1,1,1,1,0

8th Switch is on. Move ahead.
9th Switch is off. Make it on. total_changes :4


The expected output is 4. When in doubt, do make use of the expected output green button.


hi , please see it says turning on or off the switch of a particular bulb , affects the state of "all the bulbs right to it "

So its not one single bulb but all the bulbs right to it.


this will require 4 switches to be on…


Found a Simple O(N) and O(1) space solution