N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times.
Answers
Answered by
0
Answer:
Input : [0 1 0 1]
Output : 4
Explanation :
press switch 0 : [1 0 1 0]
press switch 1 : [1 1 0 1]
press switch 2 : [1 1 1 0]
press switch 3 : [1 1 1 1]
Input : [1 0 0 0 0]
Output : 1
Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.
We traverse given array from left to right and keep pressing switch for off bulbs. We keep track of the number of switch presses so far. If the number of presses are odd, that means the current switch is in its original state else it is in the other state. Depending on what state the bulb is in, we can increment the count of the number of presses.
Similar questions