Faulty wiring and Bulbs
Easy Accuracy: 60.42% Submissions: 722 Points: 2

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. "0 represents the bulb is off and 1 represents the bulb is on."

Example 1:

arr[] = { 0, 0, 0, 0 }
Output:  1
From the given set of bulbs change
the state of the first bulb from off
to on ,which eventually turn the rest
three bulbs on the right of it.

Example 2:

arr[] = { 1, 0, 0, 1 }
Output:  2

Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countFlips() that takes array A and integer N as parameters and returns the minimum number of press required to turn on all the bulbs.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).


1 ≤ N ≤ 106

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Faulty wiring and Bulbs

Output Window