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.
Note: 0 represents the bulb is off and 1 represents the bulb is on.
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
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.
The minimum number of switches needed are 4.
This article is contributed by Saloni Baweja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Minimum flips to make all 1s in left and 0s in right | Set 2
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Count the values greater than X in the modified array
- Minimum flips required to keep all 1s together in a Binary string
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Minimize the difference between the maximum and minimum values of the modified array
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Find Bit whose minimum sequence flips makes all bits same
- Queries for count of array elements with values in given range with updates
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Count minimum steps to get the given desired array
- Minimum count of numbers required from given array to represent S
- Count of all pairs in an Array with minimum absolute difference
- Count minimum factor jumps required to reach the end of an Array
- Minimum count of increment of K size subarrays required to form a given Array