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.**Examples:**

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.

`// CPP program to find number switch presses to ` `// turn all bulbs on. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; `
` ` `int` `bulbs(` `int` `a[],` `int` `n) `
`{ ` ` ` `// To keep track of switch presses so far `
` ` `int` `count = 0; `
` ` ` ` `int` `res = 0; `
` ` `for` `(` `int` `i = 0; i < n; i++) `
` ` `{ `
` ` `/* if the bulb's original state is on and count `
` ` `is even, it is currently on...*/`
` ` `/* no need to press this switch */`
` ` `if` `(a[i] == 1 && count % 2 == 0) `
` ` `continue` `; `
` ` ` ` `/* If the bulb's original state is off and count `
` ` `is odd, it is currently on...*/`
` ` `/* no need to press this switch */`
` ` `else` `if` `(a[i] == 0 && count % 2 != 0) `
` ` `continue` `; `
` ` ` ` `/* if the bulb's original state is on and count `
` ` `is odd, it is currently off...*/`
` ` `/* Press this switch to on the bulb and increase `
` ` `the count */`
` ` `else` `if` `(a[i] == 1 && count % 2 != 0) `
` ` `{ `
` ` `res++; `
` ` `count++; `
` ` `} `
` ` ` ` `/* if the bulb's original state is off and `
` ` `count is even, it is currently off...*/`
` ` `/* press this switch to on the bulb and `
` ` `increase the count */`
` ` `else` `if` `(a[i] == 0 && count % 2 == 0) `
` ` `{ `
` ` `res++; `
` ` `count++; `
` ` `} `
` ` `} `
` ` `return` `res; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `int` `states[] = {0,1,0,1}; `
` ` `int` `n = ` `sizeof` `(states)/` `sizeof` `(states[0]); `
` ` `cout << ` `"The minimum number of switches needed are "` `<< bulbs(states,n); `
`} ` ` ` `// This code is contributed by ` `// Sanjit_Prasad ` |

*chevron_right*

*filter_none*

`// Java program to find number switch presses to ` `// turn all bulbs on. ` `import` `java.util.*; `
` ` `public` `class` `GFG `
`{ ` ` ` `public` `int` `bulbs(ArrayList<Integer> a) `
` ` `{ `
` ` `// To keep track of switch presses so far `
` ` `int` `count = ` `0` `; `
` ` ` ` `int` `res = ` `0` `; `
` ` `for` `(` `int` `i = ` `0` `; i < a.size(); i++) `
` ` `{ `
` ` `/* if the bulb's original state is on and count `
` ` `is even, it is currently on...*/`
` ` `/* no need to press this switch */`
` ` `if` `(a.get(i) == ` `1` `&& count%` `2` `== ` `0` `) `
` ` `continue` `; `
` ` ` ` `/* If the bulb's original state is off and count `
` ` `is odd, it is currently on...*/`
` ` `/* no need to press this switch */`
` ` `else` `if` `(a.get(i) == ` `0` `&& count%` `2` `!= ` `0` `) `
` ` `continue` `; `
` ` ` ` `/* if the bulb's original state is on and count `
` ` `is odd, it is currently off...*/`
` ` `/* Press this switch to on the bulb and increase `
` ` `the count */`
` ` `else` `if` `(a.get(i) == ` `1` `&& count%` `2` `!= ` `0` `) `
` ` `{ `
` ` `res++; `
` ` `count++; `
` ` `} `
` ` ` ` `/* if the bulb's original state is off and `
` ` `count is even, it is currently off...*/`
` ` `/* press this switch to on the bulb and `
` ` `increase the count */`
` ` `else` `if` `(a.get(i) == ` `0` `&& count%` `2` `== ` `0` `) `
` ` `{ `
` ` `res++; `
` ` `count++; `
` ` `} `
` ` `} `
` ` `return` `res; `
` ` `} `
` ` ` ` `// Driver code `
` ` `public` `static` `void` `main(String[] args) `
` ` `{ `
` ` `GFG gfg = ` `new` `GFG(); `
` ` ` ` `ArrayList<Integer> states = ` `new` `ArrayList<Integer>(); `
` ` ` ` `states.add(` `0` `); `
` ` `states.add(` `1` `); `
` ` `states.add(` `0` `); `
` ` `states.add(` `1` `); `
` ` ` ` `System.out.println(` `"The minimum number of switches"` `+ `
` ` `" needed are "` `+ gfg.bulbs(states)); `
` ` `} `
`} ` |

*chevron_right*

*filter_none*

`# Python program to find number switch presses to ` `# turn all bulbs on. ` ` ` ` ` `def` `bulbs(a, n): `
` ` `# To keep track of switch presses so far `
` ` `count ` `=` `0`
` ` ` ` `res ` `=` `0`
` ` `for` `i ` `in` `range` `(n): `
` ` `# if the bulb's original state is on and count `
` ` `# is even, it is currently on... `
` ` `# no need to press this switch `
` ` `if` `(a[i] ` `=` `=` `1` `and` `count ` `%` `2` `=` `=` `0` `): `
` ` `continue`
` ` ` ` `# If the bulb's original state is off and count `
` ` `# is odd, it is currently on... `
` ` `# no need to press this switch `
` ` `elif` `(a[i] ` `=` `=` `0` `and` `count ` `%` `2` `!` `=` `0` `): `
` ` `continue`
` ` ` ` `# if the bulb's original state is on and count `
` ` `# is odd, it is currently off... `
` ` `# Press this switch to on the bulb and increase `
` ` `# the count `
` ` `elif` `(a[i] ` `=` `=` `1` `and` `count ` `%` `2` `!` `=` `0` `): `
` ` `res ` `+` `=` `1`
` ` `count ` `+` `=` `1`
` ` ` ` `# if the bulb's original state is off and `
` ` `# count is even, it is currently off... `
` ` `# press this switch to on the bulb and `
` ` `# increase the count `
` ` `elif` `(a[i] ` `=` `=` `0` `and` `count ` `%` `2` `=` `=` `0` `): `
` ` `res ` `+` `=` `1`
` ` `count ` `+` `=` `1`
` ` `return` `res `
` ` ` ` `# Driver code ` `states ` `=` `[` `0` `, ` `1` `, ` `0` `, ` `1` `] `
`n ` `=` `len` `(states) `
`print` `(` `"The minimum number of switches needed are"` `, bulbs(states, n)) `
` ` `# This code is contributed by ankush_953 ` |

*chevron_right*

*filter_none*

`// C# program to find number switch presses ` `// to turn all bulbs on. ` `using` `System; `
`using` `System.Collections.Generic; `
` ` `class` `GFG `
`{ ` `public` `virtual` `int` `bulbs(List<` `int` `> a) `
`{ ` ` ` `// To keep track of switch presses so far `
` ` `int` `count = 0; `
` ` ` ` `int` `res = 0; `
` ` `for` `(` `int` `i = 0; i < a.Count; i++) `
` ` `{ `
` ` `/* if the bulb's original state is on `
` ` `and count is even, it is currently on...*/`
` ` `/* no need to press this switch */`
` ` `if` `(a[i] == 1 && count % 2 == 0) `
` ` `{ `
` ` `continue` `; `
` ` `} `
` ` ` ` `/* If the bulb's original state is off `
` ` `and count is odd, it is currently on...*/`
` ` `/* no need to press this switch */`
` ` `else` `if` `(a[i] == 0 && count % 2 != 0) `
` ` `{ `
` ` `continue` `; `
` ` `} `
` ` ` ` `/* if the bulb's original state is on `
` ` `and count is odd, it is currently off...*/`
` ` `/* Press this switch to on the bulb and `
` ` `increase the count */`
` ` `else` `if` `(a[i] == 1 && count % 2 != 0) `
` ` `{ `
` ` `res++; `
` ` `count++; `
` ` `} `
` ` ` ` `/* if the bulb's original state is off and `
` ` `count is even, it is currently off...*/`
` ` `/* press this switch to on the bulb and `
` ` `increase the count */`
` ` `else` `if` `(a[i] == 0 && count % 2 == 0) `
` ` `{ `
` ` `res++; `
` ` `count++; `
` ` `} `
` ` `} `
` ` `return` `res; `
`} ` ` ` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args) `
`{ ` ` ` `GFG gfg = ` `new` `GFG(); `
` ` ` ` `List<` `int` `> states = ` `new` `List<` `int` `>(); `
` ` ` ` `states.Add(0); `
` ` `states.Add(1); `
` ` `states.Add(0); `
` ` `states.Add(1); `
` ` ` ` `Console.WriteLine(` `"The minimum number of switches"` `+ `
` ` `" needed are "` `+ gfg.bulbs(states)); `
`} ` `} ` ` ` `// This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

**Output:**

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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

https://www.interviewbit.com/problems/bulbs/

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.

## Recommended Posts:

- Minimum flips to make all 1s in left and 0s in right | Set 2
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Find Bit whose minimum sequence flips makes all bits same
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to keep all 1s together in a Binary string
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum number of flips with rotation to make binary string alternating
- Minimum substring flips required to convert given binary string to another
- Min flips of continuous characters to make all characters same in a string
- Minimize count of flips required such that no substring of 0s have length exceeding K
- Number of flips to make binary string alternate | Set 1
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve

**Practice Tags :**